2

我正在尝试使用 C++ 中的线程和互斥锁进行简单的提升。

这是代码:

#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:有错误的错误日志。哎呀。

4

1 回答 1

6

boost::mutex不是可复制或可移动(使用 boost 的 move 实现)类型,因此,以这种方式将其传递给线程将不起作用,因为该构造函数会复制函子以在线程上执行。

在您的测试用例中,您需要使用以下内容

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

这通过引用而不是通过复制传递它。注意,这也要求函子在线程退出之前保持有效。由于您要加入线程,因此这里就是这种情况,但更复杂的情况可能不是真的。

于 2013-05-03T18:50:39.407 回答