Consider the following pseudo-code in C++:
// somewhere in common code, properly scoped
boost::mutex data_ready_lock;
bool data_ready;
// Thread 1:
void SomeThreadFunc() {
// ... push data onto a shared data structure that is properly locked
data_ready_lock.lock();
data_ready = true;
data_ready_lock.unlock();
}
// Thread 2: (actually a function called from the main() thread)
// Returns the number of bytes written to output_data
size_t RequestData(uint8_t* const output_data) {
data_ready_lock.lock();
if (data_ready) {
// reset the flag, so I don't read out the same data twice
data_ready = false;
data_ready_lock.unlock();
// copy over data, etc.
return kDataSize;
} else {
data_ready_lock.unlock();
return 0;
}
}
Is there a better way to accomplish this? I was thinking about condition variables, but I need the ability to reset the flag to ensure that back to back calls to RequestData() don't yield the same data.
As always, thanks in advance for the help.