C ++提升线程的线程重用 [英] C++ boost thread reusing threads

查看:198
本文介绍了C ++提升线程的线程重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成这样的事情:

I am trying to accomplish something like this:

thread t; // create/initialize thread
t.launch(); // launch thread.
t.wait(); // wait
t.launch(); // relaunch the same thread

如何去实现这样的使用boost线程?
在本质上,我需要重新发布执着,能够线程。

How to go about implementing something like this using boost threads? in essence, I need persistent relaunch-able thread.

我想,以避免工作队列,因为在我的情况实施有一定的困难。

I would like to avoid work queue, as implemented in my case is somewhat difficult

感谢

推荐答案

您只想让在一个循环中,线程运行。它试图从队列中以工作为单位,开展的工作,然后又回到了队列中。当队列为空等待。

You would just make the thread run in a loop. It attempts to take a unit of "work" from a queue, carries out the work, and then goes back to the queue. When the queue is empty it waits.

再从您可以将工作项目到队列中另一个线程,使该线程将抓好落实。

Then from another thread you can insert work items into the queue so that the thread will carry them out.

重读你的问题,你说你希望你的主线程通知工作线程开始工作,但随后的主线程必须立即开始等待工作完成?这意味着,只有一个线程在同一时间运行过。这里就没有任何意义了这一点。螺纹设计用于并发执行。

Reading your question again, are you saying that you want your master thread to notify the worker thread to begin working, but then the master thread must immediately begin waiting for the worker to finish? This would mean that only one thread is ever running at a time. There would be no point to that. Threads are designed for concurrent execution.

假设是不是你想要的,那么我想知道有什么能比流失队列一个简单的工作线程简单。一个线程工作或者等待。所以,你需要一些数据结构,使主人和工人之间的交流,使得工人可以等待一个工作项目到达,并完成工作项目时,主机可以发送一个工作项目,这将唤醒工人,然后,工人等待一个又一个。

Assuming that isn't what you want, then I'm wondering what could be simpler than a simple worker thread running off a queue. A thread is either working or waiting. So you need some data structure that allows communication between master and worker, such that the worker can wait for a work item to arrive and the master can send a work item, which will wake up the worker, and then when the work item is completed, the worker waits for another one.

这篇关于C ++提升线程的线程重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆