]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/example/tennis.cpp
1 // Copyright (C) 2001-2003
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)
7 #undef BOOST_THREAD_VERSION
8 #define BOOST_THREAD_VERSION 2
10 #include <boost/thread/mutex.hpp>
11 #include <boost/thread/condition.hpp>
12 #include <boost/thread/thread_only.hpp>
13 #include <boost/thread/xtime.hpp>
16 #if defined(BOOST_HAS_WINTHREADS)
33 boost::condition cond
;
35 const char* player_name(int state
)
37 if (state
== PLAYER_A
)
39 if (state
== PLAYER_B
)
45 void player(int active
)
47 boost::unique_lock
<boost::mutex
> lock(mutex
);
49 int other
= active
== PLAYER_A
? PLAYER_B
: PLAYER_A
;
51 while (state
< GAME_OVER
)
53 //std::cout << player_name(active) << ": Play." << std::endl;
61 std::cout
<< "---" << player_name(active
)
62 << ": Spurious wakeup!" << std::endl
;
64 } while (state
== other
);
68 std::cout
<< player_name(active
) << ": Gone." << std::endl
;
74 thread_adapt(void (*func
)(void*), void* param
)
75 : _func(func
), _param(param
)
78 int operator()() const
91 thread_adapter(void (*func
)(void*), void* param
)
92 : _func(func
), _param(param
)
95 void operator()() const { _func(_param
); }
105 boost::thread
thrda(&player
, PLAYER_A
);
106 boost::thread
thrdb(&player
, PLAYER_B
);
109 boost::xtime_get(&xt
, boost::TIME_UTC_
);
111 boost::thread::sleep(xt
);
113 boost::unique_lock
<boost::mutex
> lock(mutex
);
114 std::cout
<< "---Noise ON..." << std::endl
;
117 for (int i
= 0; i
< 10; ++i
)
121 boost::unique_lock
<boost::mutex
> lock(mutex
);
122 std::cout
<< "---Noise OFF..." << std::endl
;
128 } while (state
!= BOTH_PLAYERS_GONE
);
131 std::cout
<< "GAME OVER" << std::endl
;