2 #include <boost/thread/mutex.hpp>
3 #include <boost/thread/thread.hpp>
7 boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe!
12 counter() : count(0) { }
15 boost::mutex::scoped_lock scoped_lock(mutex);
29 int i = c.increment();
30 boost::mutex::scoped_lock scoped_lock(io_mutex);
31 std::cout << "count == " << i << std::endl;
37 void operator() (void) {
38 int i = c.increment();
43 boost::mutex::scoped_lock scoped_lock(io_mutex);
44 std::cout << "count == " << i << std::endl;
46 virtual ~worker() { std::cout << "destroyed\n"; }
51 int main(int, char*[])
55 const int num_threads = 4;
56 boost::thread_group thrds;
58 std::list<boost::thread *> thread_list;
60 for (int i=0; i < num_threads; ++i)
62 // thrds.create_thread(&change_count);
63 worker *w = new worker;
65 boost::thread *thr = new boost::thread(*w);
67 thrds.add_thread(thr);
69 thread_list.push_back(thr);
74 std::list<boost::thread *>::iterator it;
75 for (it = thread_list.begin(); it != thread_list.end(); it++)
82 catch (std::exception &e)
84 std::cout << e.what() << "\n";
93 * indent-tabs-mode: nil
95 * vim: shiftwidth=4 tabstop=8 expandtab