1// Copyright 2022 Peter Dimov
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#include "boost/thread/scoped_thread.hpp"
6#include "boost/thread.hpp"
7#include <boost/core/lightweight_test.hpp>
8#include <iostream>
9
10static void do_first_half()
11{
12 std::cout << "Doing first half of the work\n";
13
14 boost::this_thread::sleep_for(
15 d: boost::chrono::hours( 1 ) );
16
17 std::cout << "First half of the work complete\n";
18}
19
20static void do_second_half()
21{
22 std::cout << "Doing second half of the work\n";
23
24 boost::this_thread::sleep_for(
25 d: boost::chrono::hours( 1 ) );
26
27 std::cout << "Second half of the work complete\n";
28}
29
30static void outer_thread_func()
31{
32 boost::scoped_thread<boost::interrupt_and_join_if_joinable> worker1( do_first_half );
33 boost::scoped_thread<boost::interrupt_and_join_if_joinable> worker2( do_second_half );
34
35 worker1.join();
36 worker2.join();
37}
38
39int main()
40{
41 BOOST_TEST( true ); // perform lwt initialization
42
43 std::cout << "Start" << std::endl;
44
45 {
46 boost::thread outer( outer_thread_func );
47
48 boost::this_thread::sleep_for( d: boost::chrono::seconds( 1 ) );
49
50 std::cout << "Interrupting work\n";
51
52 outer.interrupt();
53 outer.join();
54 }
55
56 std::cout << "End" << std::endl;
57
58 return boost::report_errors();
59}
60

source code of boost/libs/thread/test/test_366_4.cpp