使用atomic <bool>在两个线程上等待</bool>

时间:2014-01-14 01:06:00

标签: c++ multithreading lambda atomic

假设我有一个简单的池类:

template<typename Iterator, typename T>
class pool
{
 public:
   pool(std::vector<T> data) : 
     data_(data),
     thread_1_finished(false),
     thread_2_finished(false)
     {}
   void thread_1(Iterator, Iterator);
   void thread_2(Iterator, Iterator);
   void thread_3_method_1();
   void thread_3_method_2();
   void do_it();
 private:   
   std::vector<T> data_;
   std::mutex mut_;
   std::condition_variable data_cond_;
   threadsafe_queue<int> data_queue_;

   std::atomic<bool> thread_1_finished;
   std::atomic<bool> thread_2_finished;
};

thread_3需要等待thread_1thread_2完成,这是通过在设置thread_1_finished后调用condition_variable notify_all上的data_cond来完成的为真。 thread_1,thread_2都是这样做的:

template<typename Iterator, typename T>
  void pool<Iterator, T>::thread_1(Iterator first, Iterator last)
{
   sort_block<Iterator, T>  s;
   s(first, last);
   std::lock_guard<std::mutex> lk(mut_);
   thread_1_finished = true;
   data_cond_.notify_all();
   fprintf(stderr, "thread 1 finished.\n");
}

现在这不会编译:

template<typename Iterator, typename T>
  void pool<Iterator, T>::thread_3_method_1()
{
   std::unique_lock<std::mutex> lk(mut_);
   data_cond_.wait(lk,
           [this]()->bool
            {
              return (thread_1_finished && thread_2_finished);
           });
}

事实上我得到了:

charles@ely:~/src/C/sandbox/waiting_on_two_threads$ make
makedepend -pobj/ -Y/dev/null -I. test.cpp 2>makedepend.err
g++ -c -g -w -std=c++11 -I. -I/home/charles/src/C/include -I/home/charles/src/C    test.cpp -o obj/test.o
test.cpp: In lambda function:
test.cpp:126:38: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.7/README.Bugs> for instructions.
Preprocessed source stored into /tmp/ccU4V5nL.out file, please attach this to your     bugreport.
make: *** [obj/test.o] Error 1

编译:

template<typename Iterator, typename T>                                                             
  void pool<Iterator, T>::thread_3_method_1()                                                 
{                                                                                                   
   std::unique_lock<std::mutex> lk(mut_);                                                           
   data_cond_.wait(lk,                                                                              
               [this]()->bool                                                                   
                   {                                                                                
                      return thread_1_finished;                                                     
                   });                                                                              
   data_cond_.wait(lk,                                                                              
               [this]()->bool                                                                   
                   {                                                                                
                      return     thread_2_finished;                                                     
                  });                                                                              

}        

这是为什么?我想第二种方法会完成同样的事情,考虑到unique_lock,但我仍然没有得到它。

charles@ely:~$ uname -a
Linux ely 3.2.0-24-generic #37-Ubuntu SMP Wed Apr 25 08:43:22 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

charles@ely:~$ g++ --version
g++ (Ubuntu/Linaro 4.7.0-7ubuntu3) 4.7.0
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

0 个答案:

没有答案