【问题标题】:running boost: lock free queues globally across multiple files运行 boost:跨多个文件全局锁定无队列
【发布时间】: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);

【问题讨论】:

    标签: boost pthreads lock-free


    【解决方案1】:

    您需要在global.h 中声明而不是定义您的变量:

    extern boost::lockfree::queue<int> q;
    extern std::atomic<int> sum;
    

    然后你需要在一个单独的文件中定义它们,global.cpp:

    boost::lockfree::queue<int> q{100};
    std::atomic<int> sum{0};
    

    我认为这应该可以解决您的问题。详情见How do I use extern to share variables between source files?


    至于第二部分,问为什么你不能创建一个无锁的字符串队列,嗯,这是由错误消息回答的:has_trivial_destructor is false for std::string, because it's a dynamic-sized分配内存的字符串。您将无法在这种无锁队列中使用它。您可以尝试改用固定大小的字符串类,或std::array&lt;char, N&gt;

    【讨论】:

    • 好吧,我试过了,我有两个问题,1)似乎全局队列没有在头文件中更新。 2)另外,我不能创建一个字符串队列。
    • @ArielBaron:好的,感谢您提供的详细信息。我更新了我的答案以修复(1)。至于(2),你“不能”是什么意思?您收到错误消息了吗?
    • 对于 2) 我收到一个错误,对于 1) 总和显示为零,这表明队列没有被填充
    • @ArielBaron:“我收到错误”没有用。究竟是什么错误?粘贴整个文本。
    • 我将错误添加到问题中。感谢您的帮助!
    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2011-02-25
    • 2013-04-11
    相关资源
    最近更新 更多