Thread sleeping & waiting for a possible external trigger
Instead of the following code (CPU wasting, not ideal):
%CODE{"c++"}%
//Wait 1 minute before updating again
timer = 0;
while (timer > LIMIT_UPDATE_INTERVAL) {
SystemClock::usleep(LIMIT_STATUS_INTERVAL * 1000);
timer = timer + LIMIT_STATUS_INTERVAL;
if (this->isStopped()) break;
// If a PRESET or OFFSET is issued, recompute immediately
if (limitTrigger) break;
}
limitTrigger = false;
// and calls to trigger as (with an obvious race condition):
limitTrigger = true;
%ENDCODE%
use that (lacking isStopped part, but there will be CV added to tcs/core/Thread library):
%CODE{"c++"}%
std::condition_variable cv;
std::mutex cv_m;
int limitTrigger = 0;
....
{
std::unique_lock lk(cv_m);
cv.wait_for(lk, chrono::seconds(LIMIT_UPDATE_INTERVAL), [this] {return limitTrigger == 1;});
limitTrigger = 0;
}
// and calls to trigger as
{
std::lock_guard lk(cv_m);
limitTrigger = 1;
cv.notify_all();
}
%ENDCODE%