C++ (简单) boost thread_group 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1246813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(simple) boost thread_group question
提问by RandomGuy
I'm trying to write a fairly simple threaded application, but am new to boost's thread library. A simple test program I'm working on is:
我正在尝试编写一个相当简单的线程应用程序,但我是 boost 线程库的新手。我正在开发的一个简单的测试程序是:
#include <iostream>
#include <boost/thread.hpp>
int result = 0;
boost::mutex result_mutex;
boost::thread_group g;
void threaded_function(int i)
{
for(; i < 100000; ++i) {}
{
boost::mutex::scoped_lock lock(result_mutex);
result += i;
}
}
int main(int argc, char* argv[])
{
using namespace std;
// launch three threads
boost::thread t1(threaded_function, 10);
boost::thread t2(threaded_function, 10);
boost::thread t3(threaded_function, 10);
g.add_thread(&t1);
g.add_thread(&t2);
g.add_thread(&t3);
// wait for them
g.join_all();
cout << result << endl;
return 0;
}
However, when I compile and run this program I get an output of
但是,当我编译并运行这个程序时,我得到了一个输出
$ ./test
300000
test: pthread_mutex_lock.c:87: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
Aborted
Obviously, the result is correct but I'm worried about this error message, especially because the real program, which has essentially the same structure, is getting stuck at the join_all() point. Can someone explain to me what is happening? Is there a better way to do this, i.e. launch a number of threads, store them in a external container, and then wait for them all to complete before continuing the program?
显然,结果是正确的,但我担心这个错误消息,特别是因为具有基本相同结构的真实程序卡在了 join_all() 点。有人可以向我解释发生了什么吗?有没有更好的方法来做到这一点,即启动多个线程,将它们存储在一个外部容器中,然后在继续程序之前等待它们全部完成?
Thanks for your help.
谢谢你的帮助。
回答by VoidPointer
I think you problem is caused by the thread_group destructor which is called when your program exits. Thread group wants to take responsibility of destructing your thread objects. See also in the boost::thread_groupdocumentation.
我认为您的问题是由程序退出时调用的 thread_group 析构函数引起的。线程组要负责销毁您的线程对象。另请参阅boost::thread_group文档。
You are creating your thread objects on the stack as local variables in the scope of your main function. Thus, they have already been destructed when the program exits and thread_group tries to delete them.
您正在堆栈上创建线程对象作为主函数范围内的局部变量。因此,当程序退出并且 thread_group 尝试删除它们时,它们已经被破坏了。
As a solution, create your thread objects on the heap with newand let the thread_group take care of their destruction:
作为解决方案,使用new在堆上创建线程对象并让 thread_group 处理它们的销毁:
boost::thread *t1 = new boost::thread(threaded_function, 10);
...
g.add_thread(t1);
...
回答by Andy
If you don't need a handle to your threads, try using thread_group::create_thread() which alleviates the need to manage the thread at all:
如果您不需要线程的句柄,请尝试使用 thread_group::create_thread() ,这完全减轻了管理线程的需要:
// Snip: Same as previous examples
int main(int argc, char* argv[])
{
using namespace std;
// launch three threads
for ( int i = 0; i < 3; ++i )
g.create_thread( boost::bind( threaded_function, 10 ) );
// wait for them
g.join_all();
cout << result << endl;
return 0;
}
回答by Eugene
add_thread() takes ownership of thread you pass in. Thread group deletes the thread. In this example you are deleting memory allocated on stack, pretty much a capital offence.
add_thread() 获取您传入的线程的所有权。线程组删除线程。在这个例子中,你正在删除分配在堆栈上的内存,这几乎是一种死罪。
Member functionadd_thread()
void add_thread(thread* thrd);
Precondition:
The expression delete thrd is well-formed and will not result in undefined behaviour.
Effects:
Take ownership of the boost::thread object pointed to by thrd and add it to the group.
Postcondition:
this->size() is increased by one.
成员函数add_thread()
无效添加线程(线程* thrd);
前提:
表达式 delete thrd 格式良好,不会导致未定义的行为。
效果:
取得 thrd 指向的 boost::thread 对象的所有权并将其添加到组中。
后置条件:
this->size() 增加一。
Not sure if that's what's wrong in your code, or if this is just example bug. Otherwise code looks fine.
不确定这是否是您的代码有问题,或者这只是示例错误。否则代码看起来不错。
回答by Sean
It looks none of above actually answered the question.
看起来上面没有一个真正回答了这个问题。
I met the similar issue. The consequence of this warning (pthread_mutex_lock.c:87: __pthread_mutex_lock: Assertion `mutex->_data._owner == 0' failed. Aborted) is that sometimesthe program will leak threads and cause a boost_resource_error exception.
我遇到了类似的问题。此警告的后果(:87:__pthread_mutex_lock:pthread_mutex_lock.c断言`互斥- > _数据。_owner == 0'失败中止。)是有时程序会泄漏线程并导致boost_resource_error异常。
The reason looks like the program continues to execute after join_all() although most of threads are still running ( not terminated ).
原因看起来是程序在 join_all() 之后继续执行,尽管大多数线程仍在运行(未终止)。