小编Scl*_*ker的帖子

使用互斥锁在两个线程之间进行 C++ 同步而不使用条件变量

我有一个线程需要阻塞,直到另一个线程发生某些事情。这听起来很典型,我有这个解决方案。

//thread 1
mux.lock();
//send work to another thread
mux.lock(); //will likely block which I want

//thread 2
//get the work sent over from thread 1
//work on it, then
mux.unlock(); //unblock thread 1 - all good
Run Code Online (Sandbox Code Playgroud)

这似乎在 Linux 上运行良好,并且不需要条件变量 - 除了 C++ 标准说在同一线程中两次获取锁是未定义的行为 - 我在线程 1 中这样做。

c++ locking thread-synchronization stdmutex

2
推荐指数
1
解决办法
182
查看次数

标签 统计

c++ ×1

locking ×1

stdmutex ×1

thread-synchronization ×1