fifo3.cpp 708 Bytes
#include <deque>
#include <list>
#include <iostream>
#include <thread>
#include <mutex>
#include <unistd.h>
#include <condition_variable>
#include <chrono>

using namespace std;

std::deque<unsigned int> FIFO;
std::mutex FIFO_lock;
std::condition_variable cv;

void process()
{
  while(1)
  {
    std::unique_lock l(FIFO_lock);
    cv.wait(l, [] {return !FIFO.empty();});
    {
      do {
        cout << FIFO.back() << endl;
        FIFO.pop_back();
      } while (!FIFO.empty());
    }
  }
}

int main()
{
  std::thread t(process);
  unsigned int i = 0;
  while(1)
  {
   {
     std::unique_lock l(FIFO_lock);
     FIFO.push_front(i++);
   }
   cv.notify_one();
   std::this_thread::sleep_for(10ms);
  }
}