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
11int count = 0;
12boost::mutex mutex;
13
14void increment_count()
15{
16 boost::unique_lock<boost::mutex> lock(mutex);
17 std::cout << "count = " << ++count << std::endl;
18}
19
20boost::thread_group threads2;
21boost::thread* th2 = 0;
22
23void 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
30int main()
31{
32 {
33 boost::thread_group threads;
34 for (int i = 0; i < 3; ++i)
35 threads.create_thread(threadfunc: &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(threadfunc: &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(thrd: 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(thrd: th);
58 BOOST_TEST(threads.is_thread_in(th));
59 threads.remove_thread(thrd: 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(thrd: th2);
69 }
70 threads2.join_all();
71 }
72 return boost::report_errors();
73}
74

source code of boost/libs/thread/example/thread_group.cpp