2

別のスレッドで何かが発生するまでブロックする必要があるスレッドが 1 つあります。それは非常に典型的に聞こえますが、私はこの解決策を持っています。

//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

これは Linux では一見問題なく動作し、条件変数は必要ありません。ただし、C++ 標準では、同じスレッドでロックを 2 回取得するのは未定義の動作であるとされていますが、これはスレッド 1 で行います。

4

3 に答える 3