• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/thread.hpp>
8 #include <iostream>
9 #include <boost/detail/lightweight_test.hpp>
10 
11 int count = 0;
12 boost::mutex mutex;
13 
increment_count()14 void increment_count()
15 {
16     boost::unique_lock<boost::mutex> lock(mutex);
17     std::cout << "count = " << ++count << std::endl;
18 }
19 
20 boost::thread_group threads2;
21 boost::thread* th2 = 0;
22 
increment_count_2()23 void increment_count_2()
24 {
25     boost::unique_lock<boost::mutex> lock(mutex);
26     BOOST_TEST(threads2.is_this_thread_in());
27     std::cout << "count = " << ++count << std::endl;
28 }
29 
main()30 int main()
31 {
32   {
33     boost::thread_group threads;
34     for (int i = 0; i < 3; ++i)
35         threads.create_thread(&increment_count);
36     threads.join_all();
37   }
38 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
39   {
40     boost::thread_group threads;
41     for (int i = 0; i < 3; ++i)
42         threads.create_thread(&increment_count);
43     threads.interrupt_all();
44     threads.join_all();
45   }
46 #endif
47   {
48     boost::thread_group threads;
49     boost::thread* th = new boost::thread(&increment_count);
50     threads.add_thread(th);
51     BOOST_TEST(! threads.is_this_thread_in());
52     threads.join_all();
53   }
54   {
55     boost::thread_group threads;
56     boost::thread* th = new boost::thread(&increment_count);
57     threads.add_thread(th);
58     BOOST_TEST(threads.is_thread_in(th));
59     threads.remove_thread(th);
60     BOOST_TEST(! threads.is_thread_in(th));
61     th->join();
62     delete th;
63   }
64   {
65     {
66       boost::unique_lock<boost::mutex> lock(mutex);
67       boost::thread* th2 = new boost::thread(&increment_count_2);
68       threads2.add_thread(th2);
69     }
70     threads2.join_all();
71   }
72   return boost::report_errors();
73 }
74