本文介绍了是一个boost ::线程自动升压终止时删除:: thread_group?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有problems用我自己的线程组的实施,并成为没有接近解决,甚至识别问题上,我寻找到只用的boost :: thread_grp

I've had problems with my own "thread group" implementation, and being no closer to solving or even identifying the issue, I'm looking into just using boost::thread_grp.

现在,从什么documentation我能找到关于这个问题 ,我一直认为线程对象—不管他们的实际工作与MDASH的持续时间;仍然活着,线程组,直到线程组被销毁的一部分。

Now, from what documentation I can find on the subject, I've always believed that thread objects — no matter the duration of their actual work — remain alive and a part of a thread group until the thread group is destroyed.

但是,一个粗略的测试似乎表明,的boost :: thread_group ::大小()自身会随着线程做他们的工作并终止。这将意味着该线程对象本身正在清理对我也是。

However, a cursory test seems to indicate that boost::thread_group::size() decreases on its own as threads do their work and terminate. That would imply that the thread objects themselves are being cleaned up for me, too.

这是真的吗?我可以依靠呢?

Is this true? Can I rely on it?

#include <cassert>
#include <unistd.h>         // for sleep()
#include <boost/thread.hpp>

boost::mutex m;
unsigned int count = 0;

void func() {
   boost::mutex::scoped_lock l(m);
   count++;
}

int main()
{
   boost::thread_group grp;
   for (size_t i = 0; i < 300; i++)
      grp.create_thread(func());

   sleep(10);

   assert(count == 300);
   assert(grp.size() == 0);  // passes, in my tests

   // ^ Can I rely on that?
   //   Do I really have no thread objects eating up
   //     memory, at this point?

   grp.join_all();
   // ^ Then I guess this is doing nothing, in this case...
}

如果是,那么我的整个原始的 has_​​threads 的实施只是一个破碎的克隆,我已经浪费了我的时间。 :)

If it is, then my entire original has_threads implementation was just a broken clone and I've wasted my time. :)

- 我卡在升压1.40,但对此事的文件似乎是较新版本相同

- I'm stuck on Boost 1.40, but the documentation on the matter seems to be the same for more recent versions.

是哪里人就是一个例子说什么我的测试中表现出相反的。它也提供了一个体面的解决办法,如果是这样的话,虽然,这是很好的 thread_group 完全是线程安全的(虽然我不相信那个的shared_ptr ::得到()是线程安全的;如果什么线程之前结束的shared_ptr ::重置已完成?)。我是否需要使用这一解决方案?

This thread is an example of where people are saying the opposite of what my testing has shown. It does also provide a decent solution if that's the case, though; it's nice that thread_group is totally thread-safe (though I'm not convinced that that shared_ptr::get() is thread-safe; what if the thread ends before the shared_ptr::reset has finished?). Do I need to employ this solution?

推荐答案

没有,我misinter $ P $通过不抽象掉,对我创建线程任务调度PTED我的测试结果。总之,我被检查尺寸()上错了的boost :: thread_group

No, I misinterpreted the results of my test by not abstracting away the task scheduler that creates threads for me. In short, I was checking size() on the wrong boost::thread_group.

其实,的boost :: thread_group ::大小()的往下走它自己的线程终止,所以我马上就用<一个href=\"http://$c$creview.stackexchange.com/questions/9431/have-i-thought-of-everything-in-this-wrapper-around-boostthread-group\">an在论坛上的帖子我联系到的问题更新提供的解决方案的改编版本。

In fact, boost::thread_group::size() doesn't go down on its own as threads terminate, so I'll be using an adapted version of the solution provided in the forum post I linked to in the question update.

这篇关于是一个boost ::线程自动升压终止时删除:: thread_group?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:54
查看更多