2013-05-03 66 views
2

我正在嘗試簡單的線程和互斥體在C++中的提升。Boost:線程和函子中的互斥體

這是代碼:

#include <iostream> 

#include <boost/thread/thread.hpp> 

class mutex_test 
{ 
    private: 
     boost::mutex mut; 
    public: 
     void operator()() 
     { 
      boost::mutex::scoped_lock lock(mut); 
      std::cout << "Hi!" << std::endl; 
     } 
}; 

int main() 
{ 
    mutex_test tester; 
    boost::thread tester_thread(tester); 
    tester_thread.join(); 
    return 0; 
} 

在編譯,我得到這個錯誤:

In file included from C:\boost/boost/thread/detail/thread.hpp:15:0, 
       from C:\boost/boost/thread/thread.hpp:22, 
       from main.cpp:3: 
C:\boost/boost/thread/detail/move.hpp: In instantiation of 'typename boost::decay<T>::type boost::thread_detail::decay_copy(T&&) [with T = mutex_test&; typename boost::decay<T>::type = mutex_test]': 
C:\boost/boost/thread/detail/thread.hpp:265:88: required from 'boost::thread::thread(F&&) [with F = mutex_test&]' 
main.cpp:20:36: required from here 
C:\boost/boost/thread/detail/move.hpp:246:37: error: use of deleted function 'mutex_test::mutex_test(const mutex_test&)' 

main.cpp:5:7: note: 'mutex_test::mutex_test(const mutex_test&)' is implicitly deleted because the default definition would be ill-formed: 
main.cpp:5:7: error: use of deleted function 'boost::mutex::mutex(const boost::mutex&)' 
In file included from C:\boost/boost/thread/mutex.hpp:14:0, 
       from C:\boost/boost/thread/detail/thread.hpp:16, 
       from C:\boost/boost/thread/thread.hpp:22, 

       from main.cpp:3: 
C:\boost/boost/thread/win32/mutex.hpp:29:9: error: declared here 

In file included from C:\boost/boost/thread/thread.hpp:22:0, 
       from main.cpp:3: 
C:\boost/boost/thread/detail/thread.hpp: In instantiation of 'boost::detail::thread_data<F>::thread_data(F&&) [with F = mutex_test]': 
C:\boost/boost/thread/win32/thread_heap_alloc.hpp:100:72: required from 'T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<mutex_test>; A1 = mutex_test]' 
C:\boost/boost/thread/detail/thread.hpp:214:38: required from 'static boost::detail::thread_data_ptr boost::thread::make_thread_info(F&&) [with F = mutex_test; boost::detail::thread_data_ptr = boost::intrusive_ptr<boost::detail::thread_data_base>]' 
C:\boost/boost/thread/detail/thread.hpp:265:88: required from 'boost::thread::thread(F&&) [with F = mutex_test&]' 
main.cpp:20:36: required from here 

C:\boost/boost/thread/detail/thread.hpp:98:40: error: use of deleted function 'mutex_test::mutex_test(const mutex_test&)' 

In file included from C:\boost/boost/system/system_error.hpp:14:0, 
       from C:\boost/boost/thread/exceptions.hpp:22, 
       from C:\boost/boost/thread/win32/thread_primitives.hpp:16, 
       from C:\boost/boost/thread/win32/thread_data.hpp:11, 
       from C:\boost/boost/thread/thread.hpp:15, 
       from main.cpp:3: 
C:\boost/boost/system/error_code.hpp:214:36: warning: 'boost::system::posix_category' defined but not used [-Wunused-variable] 

C:\boost/boost/system/error_code.hpp:215:36: warning: 'boost::system::errno_ecat' defined but not used [-Wunused-variable] 
C:\boost/boost/system/error_code.hpp:216:36: warning: 'boost::system::native_ecat' defined but not used [-Wunused-variable] 

Makefile:21: recipe for target 'main.o' failed 

只有當我試圖創建函子線程出現的錯誤;直接執行它可以正常工作。 此外,如果我刪除所有對互斥鎖和鎖的引用,它可以正常工作。 E:有錯誤的錯誤日誌。哎呦。

回答

6

boost::mutex不是可複製的或可移動的(使用boost的移動實現)類型,因此將它傳遞給線程將不起作用,因爲該構造函數使得函數的副本在線程上執行。

在你的測試用例,你可能需要使用以下

boost::thread tester_thread(boost::ref(functor))

此按引用傳遞它,而不是通過副本。請注意,這也要求仿函數保持有效直到線程退出。既然你加入了這個線程,這裏就是這種情況,但是更復雜的情況可能並非如此。

+0

工作了,謝謝! – Mloc 2013-05-03 18:53:29