需要知道
boost::thread_group tgroup;
循环10次
tgroup.create_thread( boost::bind( &c , 2, 2, ) )
< ==
tgroup.join_all()
我可以在上面的< ==位置继续打印已经完成工作的线程数
答案 0 :(得分:1)
您可以使用原子计数器:查看 Live On Coliru
#include <boost/thread/thread.hpp>
#include <boost/atomic.hpp>
static boost::atomic_int running_count(20);
static void worker(boost::chrono::milliseconds effort)
{
boost::this_thread::sleep_for(effort);
--running_count;
}
int main()
{
boost::thread_group tg;
for (int i = 0, count = running_count; i < count; ++i) // count protects against data race!
tg.create_thread(boost::bind(worker, boost::chrono::milliseconds(i*50)));
while (running_count > 0)
{
std::cout << "Monitoring threads: " << running_count << " running\n";
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
tg.join_all();
}
示例输出:
Monitoring threads: 19 running
Monitoring threads: 17 running
Monitoring threads: 15 running
Monitoring threads: 13 running
Monitoring threads: 11 running
Monitoring threads: 9 running
Monitoring threads: 7 running
Monitoring threads: 5 running
Monitoring threads: 3 running
Monitoring threads: 1 running
另一种方法是使用信号量
答案 1 :(得分:0)
最简单的方法是在作业“c”结束时打印线程ID:
void c()
{
//some code here
some_safe_print << boost::this_thread::get_id();
}
这样,当线程完成时,最后一条指令就是打印它的id。