Question

I'm trying something simple with threads and mutexes in C++ with boost.

This is the code:

#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;
}

On compile, I get this error:

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

The error only occurs when I try to create a thread for the functor; executing it straight works fine. Additionally, if I remove all references to mutexes and locks it works fine.

E: Had the wrong error log. Whoops.

Was it helpful?

Solution

boost::mutex is not a copyable or moveable (using boost's move implementation) type, and hence, passing it in that way to the thread will not work, since that constructor makes a copy of the functor to execute on the thread.

In your test case, you would want to use the following

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

This passes it by reference, instead of by copy. Note, this also requires that the functor remain valid until the thread exits. Since you're joining the thread, that's the case here, but more complicated cases that might not be true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top