Commit f39aa06cfae97b030f42c7ab176e45b0b80c5938
1 parent
ebd8d90f
Three steps to FIFO
Showing
4 changed files
with
75 additions
and
3 deletions
examples09/07-fifo/fifo1_bad.cpp
0 → 100644
1 | +#include <deque> | |
2 | +#include <list> | |
3 | +#include <iostream> | |
4 | +#include <thread> | |
5 | +#include <unistd.h> | |
6 | +#include <chrono> | |
7 | + | |
8 | +using namespace std; | |
9 | + | |
10 | +std::deque<unsigned int> FIFO; | |
11 | + | |
12 | +void process() | |
13 | +{ | |
14 | + while(1) | |
15 | + { | |
16 | + if (!FIFO.empty()) | |
17 | + { | |
18 | + cout << FIFO.back() << endl; | |
19 | + FIFO.pop_back(); | |
20 | + }; | |
21 | + } | |
22 | +} | |
23 | + | |
24 | +int main() | |
25 | +{ | |
26 | + std::thread t(process); | |
27 | + unsigned int i = 0; | |
28 | + while(1) | |
29 | + { | |
30 | + FIFO.push_front(i++); | |
31 | + std::this_thread::sleep_for(10ms); | |
32 | + } | |
33 | +} | ... | ... |
examples09/07-fifo/fifo2.cpp
0 → 100644
1 | +#include <deque> | |
2 | +#include <list> | |
3 | +#include <iostream> | |
4 | +#include <thread> | |
5 | +#include <mutex> | |
6 | +#include <unistd.h> | |
7 | +#include <chrono> | |
8 | + | |
9 | +using namespace std; | |
10 | + | |
11 | +std::deque<unsigned int> FIFO; | |
12 | +std::mutex FIFO_lock; | |
13 | + | |
14 | +void process() | |
15 | +{ | |
16 | + while(1) | |
17 | + { | |
18 | + std::unique_lock l(FIFO_lock); | |
19 | + if(!FIFO.empty()) | |
20 | + { | |
21 | + cout << FIFO.back() << endl; | |
22 | + FIFO.pop_back(); | |
23 | + } | |
24 | + } | |
25 | +} | |
26 | + | |
27 | +int main() | |
28 | +{ | |
29 | + std::thread t(process); | |
30 | + unsigned int i = 0; | |
31 | + while(1) | |
32 | + { | |
33 | + { | |
34 | + std::unique_lock l(FIFO_lock); | |
35 | + FIFO.push_front(i++); | |
36 | + } | |
37 | + std::this_thread::sleep_for(10ms); | |
38 | + } | |
39 | +} | ... | ... |
examples09/07-fifo/fifo.cpp renamed to examples09/07-fifo/fifo3.cpp
examples09/07-fifo/makefile