我是 C++(在 Windows 上)和线程的新手,我目前正在尝试使用互斥锁、信号量和事件来解决我的问题。我正在尝试使用构造函数和一个名为 Enter 的方法创建一个 Barrier 类。具有唯一方法 Enter 的类 Barrier 应该阻止任何进入它的线程,直到许多线程到达该方法。在构造器处等待它接收的线程数。我的问题是如何使用锁来创造这种效果?我需要的是类似于反向信号量的东西,它可以保持线程直到达到计数,而不是像常规信号量那样让线程进入直到达到计数。关于如何解决这个问题的任何想法都会很棒。谢谢,内塔内尔。
3 回答
也许:
在 ctor 中,存储限制计数并创建一个空信号量。
当一个线程调用 Enter 时,首先锁定一个互斥锁,这样你就可以安全地在里面旋转。将线程计数增加到限制计数。如果尚未达到限制,则释放互斥体并等待信号量。如果达到限制,则在循环中发出信号量 [limit-1] 次信号,将线程计数归零,(准备好下一次),释放互斥体并从 Enter() 返回。任何在信号量上等待并且现在准备好/正在运行的线程都应该从它们的“Enter”调用中返回。
互斥锁会阻止任何已释放的线程“再次进入”,直到所有调用“Enter”并等待的线程都已设置为运行并且屏障被重置。
您可以使用条件变量来实现它。
这是一个例子:
我声明了 25 个线程并启动它们执行 WorkerThread 函数。
我检查阻塞/取消线程的条件是该部分中的线程数是否小于 2。(我添加了一些断言来证明我的代码做了什么)。
我的代码只是在临界区休眠,并且在我减少了临界区的线程数之后。
我还为 cout 添加了一个互斥锁以获取干净的消息。#include #include #include #include #include #include #include /* assert */ using namespace std;
std::mutex m;
atomic<int> NumThreadsInCritialSection=0;
int MaxNumberThreadsInSection=2;
std::condition_variable cv;
mutex coutMutex;
int WorkerThread()
{
// Wait until main() sends data
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return NumThreadsInCritialSection<MaxNumberThreadsInSection;});
}
assert (NumThreadsInCritialSection<MaxNumberThreadsInSection);
assert (NumThreadsInCritialSection>=0);
NumThreadsInCritialSection++;
{
std::unique_lock<std::mutex> lk(coutMutex);
cout<<"NumThreadsInCritialSection= "<<NumThreadsInCritialSection<<endl;
}
std::this_thread::sleep_for(std::chrono::seconds(5));
NumThreadsInCritialSection--;
{
std::unique_lock<std::mutex> lk(coutMutex);
cout<<"NumThreadsInCritialSection= "<<NumThreadsInCritialSection<<endl;
}
cv.notify_one();
return 0;
}
int main()
{
vector<thread> vWorkers;
for (int i=0;i<25;++i)
{
vWorkers.push_back(thread(WorkerThread));
}
for (auto j=vWorkers.begin(); j!=vWorkers.end(); ++j)
{
j->join();
}
return 0;
}
希望对您有所帮助,如果您有任何问题,请告诉我,我可以评论或更改我的代码。
伪代码大纲可能如下所示:
void Enter()
{
Increment counter (atomically or with mutex)
if(counter >= desired_count)
{
condition_met = true; (protected if bool writes aren't atomic on your architecture)
cond_broadcast(blocking_cond_var);
}
else
{
Do a normal cond_wait loop-plus-predicate-check (waiting for the broadcast and checking condition_met each iteration to protect for spurious wakeups).
}
}