
Originally Posted by
Lindley
Arguably the two most important things to be familiar with in any threading library are mutexes and condition variables. The former allow you to protect important data structures and variables from simultaneous access; the latter allows you to efficiently cause one thread to wait for a particular event to happen in another thread.
Both will probably be needed to define a reader/writer queue mechanism correctly.
mrgr8avill, you need something along the lines of:
Code:
class request_queue
{
public:
void enqueue(request* req)
{
bool notify = false;
{
std::lock_guard lock (mtx);
notify = queue.empty();
queue.push_back(req);
}
if (notify)
cv.notify_one();
}
request* dequeue()
{
std::lock_guard lock (mtx);
while (queue.empty())
cv.wait(lock);
request* req = queue.front();
queue.pop_front();
return req;
}
private:
std::mutex mtx;
std::condition_variable cv;
std::queue<request*> queue;
};