【发布时间】:2017-02-17 17:51:04
【问题描述】:
我想从这个网站运行示例 46.3 的变体 http://theboostcpplibraries.com/boost.lockfree。我在linux系统上。
我想在头文件中定义队列 q。我想让生产和消费功能在不同的文件中。所以我想让 global.h 包含
static boost::lockfree::queue<int> q{100};
static std::atomic<int> sum{0};
void *produce (void*);
void *consume (void*);
然后我想要一个produce.cpp包含:
void *produce( void*)
{
for (int i = 1; i <= 10000; ++i)
q.push(i);
}
我想要一个consume.cpp包含
void *consume (void*)
{
int i;
while (q.pop(i))
sum += i;
}
然后我想让我的 main.cpp 包含
#include iosteam
#include iomanip
#include global
#include pthread
int main ()
{pthread_t t1;
pthread_t t2;
pthread_t t3;
int t1_iret;
t1_iret = pthread_create( &t1, NULL, produce, NULL);
if(t1_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t1_iret);
exit(EXIT_FAILURE);
}
int t2_iret;
t2_iret = pthread_create( &t2, NULL, consume, NULL);
if(t2_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t2_iret);
exit(EXIT_FAILURE);
}
int t3_iret;
t3_iret = pthread_create( &t3, NULL, consume, NULL);
if(t3_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t3_iret);
exit(EXIT_FAILURE);
}
pthread_join( t1, NULL);
pthread_join( t2, NULL);
pthread_join( t3, NULL);
return 0; }
此外,我想知道是否可以使用字符串而不是整数来执行我所描述的操作。
edit1:当我尝试使队列成为字符串队列时,我得到::
/usr/local/include/boost/lockfree/queue.hpp:在“class boost::l ockfree::queue >”的实例化中: /home/ubuntu/Project/src/main.cpp:15:37:从这里需要 /usr/local/include/boost/lockfree/queue.hpp:87:5:错误:静态断言失败:(boost::has_trivial_destructor::value) BOOST_STATIC_ASSERT((boost::has_trivial_destructor::value)); ^ /usr/local/include/boost/lockfree/queue.hpp:91:5:错误:静态断言失败:(boost::has_trivial_assign::value) BOOST_STATIC_ASSERT((boost::has_trivial_assign::value)); ^ 在 /usr/local/include/boost/lockfree/queue.hpp:21:0 包含的文件中, 来自/home/ubuntu/Project/src/main.cpp:5: /usr/local/include/boost/lockfree/detail/copy_payload.hpp: 在 ' static void boost::lockfree::detail::copy_constructible_and_copyable::copy(T&, U &) 的实例化中 [with T = std::basic_string ; U = int]': /usr/local/include/boost/lockfree/detail/copy_payload.hpp:49:25: 需要来自'void boost::lockfree::detail::copy_payload(T&, U&) [with T = std::basic_string; U = int]' /usr/local/include/boost/lockfree/queue.hpp:402:61: 来自'bool boost::lockfree::queue::pop(U&) [with U = int; T = std::basic_string; A0 = 升压::参数::void_; A1 = 升压::参数::void_; A2 = boost::parameter::void_]’ /home/ubuntu/Project/src/main.cpp:21:24:从这里需要 /usr/local/include/boost/lockfree/detail/copy_payload.hpp:38:11:错误:从“std::basic_string”类型到“int”类型的无效转换 u = U(t);
【问题讨论】: