私の現在のコードは次のようになります
void XXX::waitForUpdates()
{
boost::unique_lock<boost::mutex> lock(mutex_agentDone);
while(!allAgentUpdatesDone()) {
COND_VAR_AGENT_DONE.wait(lock);
}
}
void XXX::onAgentUpdate(YYY argums){
Agent * target = const_cast<Agent*>(argums.GetAgent());
boost::unique_lock<boost::mutex> lock(mutex_agentDone);
REGISTERED_AGENTS.setDone(target,true);
COND_VAR_AGENT_DONE.notify_all();
}
onAgentUpdateが1 秒間に約 100 万回呼び出される場合を除いて、すべて問題ありません。パフォーマンスと最適化について心配する必要があります。
したがって、チェックを行うwait(lock)totimed_waitバージョンを変更すると、毎秒数十万単位で呼び出される s をallAgentUpdatesDone()スキップできると考えました。.notify()あえぎません、これはシミュレーション フレームワークです :)
次に、myseld に尋ねました。何のために が必要mutex_agentDoneですか? 次のように 2 つの関数を変更できます。
void XXX::waitForUpdates()
{
//this lock will become practically useless, coz there is no other
// mutex_agentDone being locked in any other function.
boost::unique_lock<boost::mutex> lock(mutex_agentDone);
while(!allAgentUpdatesDone()) {
COND_VAR_AGENT_DONE.timed_wait(lock,some_time_interval);
}
}
void XXX::onAgentUpdate(YYY argums){
Agent * target = const_cast<Agent*>(argums.GetAgent());
REGISTERED_AGENTS.setDone(target,true)
}
問題は、これは安全ですか?
ありがとうございました
ちょっとしたメモ: 2 つの関数の残りの操作は、独自のミューテックスによって既に保護されていると仮定します (は、すべてのアクセサーと反復メソッドで呼び出される独自のミューテックスREGISTERED_AGENTSを持つクラス オブジェクトであるため、REGISTERED_AGENTSと同じものを使用して同じことを反復しています)containermutexallAgentUpdatesDone()containermutex