1 | #include <queue>
|
---|
2 |
|
---|
3 | #include <boost/atomic.hpp>
|
---|
4 | #include <boost/bind.hpp>
|
---|
5 | #include <boost/coroutine/all.hpp>
|
---|
6 | #include <boost/lockfree/queue.hpp>
|
---|
7 | #include <boost/thread.hpp>
|
---|
8 |
|
---|
9 | typedef boost::coroutines::coroutine< void > coro_t;
|
---|
10 |
|
---|
11 | static void foo( coro_t::push_type& yield, int i )
|
---|
12 | {}
|
---|
13 |
|
---|
14 | struct Worker
|
---|
15 | {
|
---|
16 | boost::atomic< bool > done;
|
---|
17 |
|
---|
18 | Worker() :
|
---|
19 | done( false)
|
---|
20 | {}
|
---|
21 |
|
---|
22 | void operator()()
|
---|
23 | {
|
---|
24 | while ( ! done)
|
---|
25 | {
|
---|
26 | pending.consume_all( [&]( int i)
|
---|
27 | {
|
---|
28 | coro_t::pull_type( boost::bind( foo, _1, i) );
|
---|
29 | } );
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | boost::lockfree::queue< int, boost::lockfree::capacity< 1024 > > pending;
|
---|
34 | };
|
---|
35 |
|
---|
36 | int main( int argc, char * argv[])
|
---|
37 | {
|
---|
38 | for ( int i = 1; i < 1000; ++i)
|
---|
39 | {
|
---|
40 | Worker workers[2];
|
---|
41 | boost::thread threads[2] = {
|
---|
42 | boost::thread( boost::ref( workers[0]) ),
|
---|
43 | boost::thread( boost::ref( workers[1]) )
|
---|
44 | };
|
---|
45 | workers[0].pending.push(0);
|
---|
46 | workers[1].pending.push(1);
|
---|
47 |
|
---|
48 | workers[0].done = true;
|
---|
49 | workers[1].done = true;
|
---|
50 |
|
---|
51 | threads[0].join();
|
---|
52 | threads[1].join();
|
---|
53 | }
|
---|
54 | return EXIT_SUCCESS;
|
---|
55 | }
|
---|