【发布时间】:2023-03-10 16:32:02
【问题描述】:
我正在尝试编写一个程序,该程序将在循环中不断运行,并且仅在前一个线程已关闭时才运行一个线程。我无法在第一个 if 语句之外检查线程的状态,因为 status 是在第一个 if 语句中声明的。如果我检查第一条语句中的状态,我会被完全锁定。如何在不使线程加入主程序的情况下实现一些解决此问题的方法?
int script_lock = 1; //lock is open
while (true) {
if ( script_lock == 1) {
script_lock = 0; //lock is closed
auto future = async (script, execute); //runs concurrently with main program
auto status = future.wait_for(chrono::milliseconds(0));
}
if (status == future_status::ready) { //status not declared in scope
script_lock = 1; //lock is open
}
//do extra stuff
}
【问题讨论】:
-
你不能在
while之后声明status吗?
标签: c++ multithreading pthreads