【发布时间】:2018-11-13 00:01:09
【问题描述】:
我有一个启动 N 个线程(异步/未来)的程序。我想让主线程设置一些数据,然后所有线程都应该去,而主线程等待所有其他线程完成,然后这需要循环。
我的atm是这样的
int main()
{
//Start N new threads (std::future/std::async)
while(condition)
{
//Set Up Data Here
//Send Data to threads
{
std::lock_guard<std::mutex> lock(mrun);
bRun = true;
}
run.notify_all();
//Wait for threads
{
std::unique_lock<std::mutex> lock(mrun);
run.wait(lock, [] {return bDone; });
}
//Reset bools
bRun = false;
bDone = false;
}
//Get results from futures once complete
}
int thread()
{
while(otherCondition)
{
std::unique_lock<std::mutex> lock(mrun);
run.wait(lock, [] {return bRun; });
bDone = true;
//Do thread stuff here
lock.unlock();
run.notify_all();
}
}
但是我看不到任何主线程或其他线程在等待对方的迹象!知道我做错了什么或者我该怎么做吗?
【问题讨论】:
标签: c++ multithreading asynchronous condition-variable