Cpp中的QThread屏障机制

问题描述 投票:0回答:1

如何在Qthread run()方法中定义障碍点以进行同步。我的run方法代码包含两个阶段,所有线程必须经过第一阶段的末尾才能通过第二阶段。

void ThreadClass::run()
{

    barrier// All of the thread must reach this point before passing below the line

}
qt qthread barrier
1个回答
0
投票

从我的头顶:::>

1:创建一个互斥锁,然后在创建线程池之前将其锁定在main()中。创建线程池,让它们运行,您的障碍应如下所示。

perThreadReachedThisPointFlag = 1;
mutexCreatedByMain.lock();
mutexCreatedByMain.unlock();

在您的main()中,监视线程池。如果观察到(不要忘记内存隔离栅)池中的所有线程都已设置了perThreadReachedThisPointFlag,则执行mutexCreatedByMain.unlock();。在您的main()上。

所有线程都在等待锁定所提到的互斥锁,然后您放开它们。它们都将锁定然后解锁互斥锁。

2:另一种方式是使用pthread的conditionVariable和conditionSignal功能,但我不知道Windows的替代品。

© www.soinside.com 2019 - 2024. All rights reserved.