fifo3.cpp
708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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);
}
}