ここで作業するコアが少なくとも 2 つあると仮定します。そうでなければ、マルチスレッドは役に立ちません。ここでは C++11 マルチスレッド セマンティクスを使用しているため、コンパイラで C++11 言語仕様を有効にする必要があります。
#include <condition_variable>
#include <thread>
#include <mutex>
using namespace std;
condition_variable cv;
mutex mtx;
bool ready = false;
void update_vars() {
while( true ) {
// Get a unique lock on the mutex
unique_lock<mutex> lck(mtx);
// Wait on the condition variable
while( !ready ) cv.await( mtx );
// When we get here, the condition variable has been triggered and we hold the mutex
// Do non-threadsafe stuff
ready = false;
// Do threadsafe stuff
}
}
void do_stuff() {
while( true ) {
// Do stuff on vars
if ( f(vars) ) {
// Lock the mutex associated with the condition variable
unique_lock<mutex> lck(mtx);
// Let the other thread know we're ready for it
ready = true;
// and signal the condition variable
cv.signal_all();
}
while( ready ) {
// Active wait while update_vars does non-threadsafe stuff
}
}
}
int main() {
thread t( update_vars );
do_stuff()
}
上記のコード スニペットが行うことは、update vars を実行するセカンダリ スレッドを作成することです。これはハングアップし、メイン スレッド (do_stuff を実行している) が条件変数を介してシグナルを送信するまで待機します。
PS、おそらく先物でもこれを行うことができますが、それらに基づいて答えるのに十分な作業をしていません。