[12] | 1 | // Copyright (C) 2001-2003 |
---|
| 2 | // William E. Kempf |
---|
| 3 | // |
---|
| 4 | // Permission to use, copy, modify, distribute and sell this software |
---|
| 5 | // and its documentation for any purpose is hereby granted without fee, |
---|
| 6 | // provided that the above copyright notice appear in all copies and |
---|
| 7 | // that both that copyright notice and this permission notice appear |
---|
| 8 | // in supporting documentation. William E. Kempf makes no representations |
---|
| 9 | // about the suitability of this software for any purpose. |
---|
| 10 | // It is provided "as is" without express or implied warranty. |
---|
| 11 | |
---|
| 12 | #include <boost/thread/mutex.hpp> |
---|
| 13 | #include <boost/thread/condition.hpp> |
---|
| 14 | #include <boost/thread/thread.hpp> |
---|
| 15 | #include <boost/thread/xtime.hpp> |
---|
| 16 | #include <iostream> |
---|
| 17 | |
---|
| 18 | #if defined(BOOST_HAS_WINTHREADS) |
---|
| 19 | # include <windows.h> |
---|
| 20 | # include <process.h> |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | enum game_state |
---|
| 24 | { |
---|
| 25 | START, |
---|
| 26 | PLAYER_A, |
---|
| 27 | PLAYER_B, |
---|
| 28 | GAME_OVER, |
---|
| 29 | ONE_PLAYER_GONE, |
---|
| 30 | BOTH_PLAYERS_GONE |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | int state; |
---|
| 34 | boost::mutex mutex; |
---|
| 35 | boost::condition cond; |
---|
| 36 | |
---|
| 37 | char* player_name(int state) |
---|
| 38 | { |
---|
| 39 | if (state == PLAYER_A) |
---|
| 40 | return "PLAYER-A"; |
---|
| 41 | if (state == PLAYER_B) |
---|
| 42 | return "PLAYER-B"; |
---|
| 43 | throw "bad player"; |
---|
| 44 | return 0; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void player(void* param) |
---|
| 48 | { |
---|
| 49 | boost::mutex::scoped_lock lock(mutex); |
---|
| 50 | |
---|
| 51 | int active = (int)param; |
---|
| 52 | int other = active == PLAYER_A ? PLAYER_B : PLAYER_A; |
---|
| 53 | |
---|
| 54 | while (state < GAME_OVER) |
---|
| 55 | { |
---|
| 56 | std::cout << player_name(active) << ": Play." << std::endl; |
---|
| 57 | state = other; |
---|
| 58 | cond.notify_all(); |
---|
| 59 | do |
---|
| 60 | { |
---|
| 61 | cond.wait(lock); |
---|
| 62 | if (state == other) |
---|
| 63 | { |
---|
| 64 | std::cout << "---" << player_name(active) |
---|
| 65 | << ": Spurious wakeup!" << std::endl; |
---|
| 66 | } |
---|
| 67 | } while (state == other); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | ++state; |
---|
| 71 | std::cout << player_name(active) << ": Gone." << std::endl; |
---|
| 72 | cond.notify_all(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | struct thread_adapt |
---|
| 76 | { |
---|
| 77 | thread_adapt(void (*func)(void*), void* param) |
---|
| 78 | : _func(func), _param(param) |
---|
| 79 | { |
---|
| 80 | } |
---|
| 81 | int operator()() const |
---|
| 82 | { |
---|
| 83 | _func(_param); |
---|
| 84 | return 0; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void (*_func)(void*); |
---|
| 88 | void* _param; |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | class thread_adapter |
---|
| 92 | { |
---|
| 93 | public: |
---|
| 94 | thread_adapter(void (*func)(void*), void* param) |
---|
| 95 | : _func(func), _param(param) |
---|
| 96 | { |
---|
| 97 | } |
---|
| 98 | void operator()() const { _func(_param); } |
---|
| 99 | private: |
---|
| 100 | void (*_func)(void*); |
---|
| 101 | void* _param; |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | int main(int argc, char* argv[]) |
---|
| 105 | { |
---|
| 106 | state = START; |
---|
| 107 | |
---|
| 108 | boost::thread thrda(thread_adapter(&player, (void*)PLAYER_A)); |
---|
| 109 | boost::thread thrdb(thread_adapter(&player, (void*)PLAYER_B)); |
---|
| 110 | |
---|
| 111 | boost::xtime xt; |
---|
| 112 | boost::xtime_get(&xt, boost::TIME_UTC); |
---|
| 113 | xt.sec += 1; |
---|
| 114 | boost::thread::sleep(xt); |
---|
| 115 | { |
---|
| 116 | boost::mutex::scoped_lock lock(mutex); |
---|
| 117 | std::cout << "---Noise ON..." << std::endl; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | for (int i = 0; i < 1000000; ++i) |
---|
| 121 | cond.notify_all(); |
---|
| 122 | |
---|
| 123 | { |
---|
| 124 | boost::mutex::scoped_lock lock(mutex); |
---|
| 125 | std::cout << "---Noise OFF..." << std::endl; |
---|
| 126 | state = GAME_OVER; |
---|
| 127 | cond.notify_all(); |
---|
| 128 | do |
---|
| 129 | { |
---|
| 130 | cond.wait(lock); |
---|
| 131 | } while (state != BOTH_PLAYERS_GONE); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | std::cout << "GAME OVER" << std::endl; |
---|
| 135 | |
---|
| 136 | thrda.join(); |
---|
| 137 | thrdb.join(); |
---|
| 138 | |
---|
| 139 | return 0; |
---|
| 140 | } |
---|