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 #define BOOST_THREAD_VERSION 2
9 #include <boost/thread/mutex.hpp>
10 #include <boost/thread/condition.hpp>
11 #include <boost/thread/thread_only.hpp>
12 #include <boost/thread/xtime.hpp>
24 canteen() : m_chickens(0) { }
28 boost::unique_lock<boost::mutex> lock(m_mutex);
29 while (m_chickens == 0)
32 boost::unique_lock<boost::mutex> lk(iomx);
33 std::cout << "(" << clock() << ") Phil" << id <<
34 ": wot, no chickens? I'll WAIT ..." << std::endl;
36 m_condition.wait(lock);
39 boost::unique_lock<boost::mutex> lk(iomx);
40 std::cout << "(" << clock() << ") Phil" << id <<
41 ": those chickens look good ... one please ..." << std::endl;
47 boost::unique_lock<boost::mutex> lock(m_mutex);
49 boost::unique_lock<boost::mutex> lk(iomx);
50 std::cout << "(" << clock()
51 << ") Chef: ouch ... make room ... this dish is "
52 << "very hot ..." << std::endl;
55 boost::xtime_get(&xt, boost::TIME_UTC_);
57 boost::thread::sleep(xt);
60 boost::unique_lock<boost::mutex> lk(iomx);
61 std::cout << "(" << clock() <<
62 ") Chef: more chickens ... " << m_chickens <<
63 " now available ... NOTIFYING ..." << std::endl;
65 m_condition.notify_all();
70 boost::condition m_condition;
78 const int chickens = 4;
80 boost::unique_lock<boost::mutex> lock(iomx);
81 std::cout << "(" << clock() << ") Chef: starting ..." << std::endl;
86 boost::unique_lock<boost::mutex> lock(iomx);
87 std::cout << "(" << clock() << ") Chef: cooking ..." << std::endl;
90 boost::xtime_get(&xt, boost::TIME_UTC_);
92 boost::thread::sleep(xt);
94 boost::unique_lock<boost::mutex> lock(iomx);
95 std::cout << "(" << clock() << ") Chef: " << chickens
96 << " chickens, ready-to-go ..." << std::endl;
98 g_canteen.put(chickens);
104 phil(int id) : m_id(id) { }
107 boost::unique_lock<boost::mutex> lock(iomx);
108 std::cout << "(" << clock() << ") Phil" << m_id
109 << ": starting ..." << std::endl;
116 boost::xtime_get(&xt, boost::TIME_UTC_);
118 boost::thread::sleep(xt);
121 boost::unique_lock<boost::mutex> lk(iomx);
122 std::cout << "(" << clock() << ") Phil" << m_id
123 << ": gotta eat ..." << std::endl;
127 boost::unique_lock<boost::mutex> lk(iomx);
128 std::cout << "(" << clock() << ") Phil" << m_id
129 << ": mmm ... that's good ..." << std::endl;
133 static void do_thread(void* param) {
134 static_cast<phil*>(param)->run();
142 thread_adapt(void (*func)(void*), void* param)
143 : _func(func), _param(param)
146 int operator()() const
152 void (*_func)(void*);
159 thread_adapter(void (*func)(void*), void* param)
160 : _func(func), _param(param)
163 void operator()() const { _func(_param); }
165 void (*_func)(void*);
171 boost::thread thrd_chef(&chef);
172 phil p[] = { phil(0), phil(1), phil(2), phil(3), phil(4) };
173 boost::thread thrd_phil0(thread_adapter(&phil::do_thread, &p[0]));
174 boost::thread thrd_phil1(thread_adapter(&phil::do_thread, &p[1]));
175 boost::thread thrd_phil2(thread_adapter(&phil::do_thread, &p[2]));
176 boost::thread thrd_phil3(thread_adapter(&phil::do_thread, &p[3]));
177 boost::thread thrd_phil4(thread_adapter(&phil::do_thread, &p[4]));