这是我的示例代码:
void bar(int node, int value1, int value2)
{
// do stuff...
}
int main()
{
std::thread node1 (bar, 1, 10, 12);
std::thread node2 (bar, 2, 21, 23);
std::cout << "main, node 1 and 2 now execute concurrently...\n";
// synchronize threads:
node1.join(); // pauses until first finishes
node2.join(); // pauses until second finishes
std::cout << "completed.\n";
return 0;
}
有什么方法可以找到具有值的线程是否正在运行或存在于池中?示例:查找函数栏中节点值为 1 的线程是否正在运行?或者,名为 node1 的线程正在运行?
谢谢
最佳答案
每个线程都有一个值,一个标识符,那么可以做的就是可以做thread.id()
来标识一个线程,并存储在一个数组中,
代码:
int tid [2];
thread t [2];
for(int i=0;i <2;i++)
{
tid [i] = t [i].id ()
}
然后写一个搜索函数来匹配线程和它的id
关于c++ - 找到有值(value)的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47079062/