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