2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-22 17:52:18 +00:00
Files
thread/example/monitor/monitor.cpp
2001-06-26 19:20:11 +00:00

102 lines
2.0 KiB
C++

#include <vector>
#include <iostream>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
namespace {
const int ITERS = 100;
boost::mutex io_mutex;
};
template <typename M>
class buffer_t : public M, public boost::condition
{
public:
typedef typename M::lock lock;
buffer_t(int n)
: p(0), c(0), full(0), buf(n)
{
}
void send(int m)
{
lock lk(*this);
while (full == buf.size())
wait(lk);
buf[p] = m;
p = (p+1) % buf.size();
++full;
notify_all();
}
int receive()
{
lock lk(*this);
while (full == 0)
wait(lk);
int i = buf[c];
c = (c+1) % buf.size();
--full;
notify_all();
return i;
}
static buffer_t& get_buffer()
{
static buffer_t buf(2);
return buf;
}
static void do_sender_thread()
{
for (int n = 0; n < ITERS; ++n)
{
get_buffer().send(n);
{
volatile boost::mutex::lock lock(io_mutex);
std::cout << "sent: " << n << std::endl;
}
}
}
static void do_receiver_thread()
{
int n;
do
{
n = get_buffer().receive();
{
volatile boost::mutex::lock lock(io_mutex);
std::cout << "received: " << n << std::endl;
}
} while (n < ITERS - 1);
}
private:
unsigned int p, c, full;
std::vector<int> buf;
};
template <typename M>
void do_test(M* dummy=0)
{
typedef buffer_t<M> buffer_type;
buffer_type::get_buffer();
boost::thread::create(&buffer_type::do_sender_thread);
boost::thread::create(&buffer_type::do_receiver_thread);
boost::thread::join_all();
}
void test_buffer()
{
do_test<boost::mutex>();
do_test<boost::recursive_mutex>();
}
int main()
{
test_buffer();
return 0;
}