gpt4 book ai didi

c++ - Boost:仿函数中的线程和互斥锁

转载 作者:搜寻专家 更新时间:2023-10-31 00:16:21 26 4
gpt4 key购买 nike

我正在使用 boost 在 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: 有错误的错误日志。糟糕。

最佳答案

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

在您的测试用例中,您可能希望使用以下内容

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

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

关于c++ - Boost:仿函数中的线程和互斥锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16365561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com