| 1 | #include <boost/atomic.hpp>
|
|---|
| 2 | #include <boost/chrono.hpp>
|
|---|
| 3 | #include <boost/thread.hpp>
|
|---|
| 4 | #include <vector>
|
|---|
| 5 |
|
|---|
| 6 | int main()
|
|---|
| 7 | {
|
|---|
| 8 | #if ! defined BOOST_NO_CXX11_LAMBDAS
|
|---|
| 9 | boost::atomic<bool> done(false);
|
|---|
| 10 | std::vector<boost::thread> workers;
|
|---|
| 11 | boost::timed_mutex m;
|
|---|
| 12 | workers.reserve(128);
|
|---|
| 13 | for(size_t n=0; n<workers.capacity(); n++)
|
|---|
| 14 | workers.emplace_back([&](size_t idx){
|
|---|
| 15 | if(idx & 1)
|
|---|
| 16 | {
|
|---|
| 17 | while(!done)
|
|---|
| 18 | {
|
|---|
| 19 | if(m.try_lock_for(boost::chrono::microseconds(1)))
|
|---|
| 20 | {
|
|---|
| 21 | m.unlock();
|
|---|
| 22 | //printf("L %u ", (unsigned) idx);
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 | else
|
|---|
| 27 | {
|
|---|
| 28 | auto now=boost::chrono::steady_clock::now();
|
|---|
| 29 | while(!done)
|
|---|
| 30 | {
|
|---|
| 31 | if(m.try_lock_until(now))
|
|---|
| 32 | {
|
|---|
| 33 | m.unlock();
|
|---|
| 34 | //printf("L %u ", (unsigned) idx);
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | }, n);
|
|---|
| 39 | boost::this_thread::sleep_for(boost::chrono::seconds(30));
|
|---|
| 40 | done=true;
|
|---|
| 41 | for(auto &i : workers)
|
|---|
| 42 | i.join();
|
|---|
| 43 | #endif
|
|---|
| 44 | return 0;
|
|---|
| 45 | }
|
|---|