1. Overview
A thread is a discrete processing queue that allows different functions to be executed within the same program at the same time. This makes it very important for a function that performs some special operation for a long time without interrupting other functions during execution. The thread actually allows two types of functions to be executed simultaneously, and the two functions do not have to wait for each other.
Once an application is started, it contains only one default thread. This thread executes the main () function. The function called in main () is executed in the context sequence of this thread. Such a program is called a single-threaded program.
On the contrary, programs that create new threads are multi-threaded programs. They can not only execute multiple functions at the same time, but also play an important role in the era of multi-core prevalence. Since multiple cores allow simultaneous execution of multiple functions, this requires developers to use such processing capabilities accordingly. However, threads have been used to execute multiple functions concurrently, and developers have to carefully construct applications to support such concurrency. Multi-threaded programming knowledge has become increasingly important in the multi-core system era.
2. Thread Management2.1 scenario 1 (test_thread_wait1)
The most important class in this library is boost: thread, which is defined in boost/thread. HPP and used to create a new thread. The following example shows how to use it.
The name of the function executed in the new thread is passed to the boost: thread constructor. Once the variable t in the preceding example is created, the thread () function is executed immediately in the thread where it is located. The threadfun1 () is also concurrently executed in test_thread_wait1 ().
To prevent program termination, you need to call the join () method for the new thread. The join () method is a blocking call: It can pause the current thread until the end of the thread that calls join. This causes the test_thread_wait1 () function to wait until the end of threadfun1.
As shown in the preceding example, a specific thread can be accessed through a variable such as t, and the variable waits for its termination using the join () method. However, the thread will continue to execute even if T is out of bounds or destructed. A thread is always bound to a boost: thread variable at the beginning, but once created, it does not depend on it. There is even a method called detach () that allows variables of the boost: thread type to be separated from the corresponding thread. Of course, the join () method cannot be called because the variable is no longer a valid thread.
What can be done in any function can also be done in one thread. In the final analysis, a thread is just a function, except that it is executed simultaneously. In the preceding example, five numbers are written into the standard output stream in a loop. To reduce the output speed, calling the wait () function in each loop delays the execution by one second. Wait () can call a function named sleep (), which also comes from boost. Thread and is located in the boost: this_thread namespace.
Sleep () allows the thread to continue execution after a specified period of time or a specific time point. By passing an object of the boost: posix_time: seconds type, we specify a period of time in this example. Boost: posix_time: seconds comes from the boost. datetime library. It is used by boost. thread to manage and process time data.
Although the previous example shows how to wait for a different thread, the following example shows how to interrupt a thread through a so-called Central breakpoint.
2.2 scenario 2 (test_thread_wait2 ())
Calling interrupt () on a thread object will interrupt the corresponding thread. In this regard, interruption means an exception of the type Boost: thread_interrupted, which will be thrown in this thread. This occurs only when the thread reaches the interrupted point.
A simple call to interrupt () does not work if the given thread does not contain any breakpoints. Every time a breakpoint occurs in a thread, it checks whether interrupt () has been called. A boost: thread_interrupted exception is thrown only when it is called.
Boost. Thread defines a series of medium breakpoints, such as the sleep () function. As sleep () is called five times in this example, the thread checks whether it should be interrupted for five times. However, calls between sleep () cannot interrupt the thread.
Once the program is executed, it prints only three numbers to the standard output stream. This is because the interrupt () method is called 3 seconds later in test_thread_wait2. Therefore, the corresponding thread is interrupted and a boost: thread_interrupted exception is thrown. This exception is also correctly captured in the thread, although catch processing is empty. Because the thread () function is returned after the processing program, the thread is also terminated. In turn, the entire program will be terminated because test_thread_wait2 () waits for the thread to terminate the thread using join.
The boost. Thread definition includes ten interruptions of the preceding sleep () function. With these breakpoints, the thread can be easily interrupted in a timely manner. However, they are not always the best choice, because the breakpoint must be read beforehand to check for Boost: thread_interrupted exceptions.
3. Example
Void wait (INT seconds) {boost: this_thread: Sleep (boost: posix_time: seconds (seconds);} void threadfun1 () {for (INT I = 0; I <5; ++ I) {Wait (1); print_debug (I) ;}} void threadfun2 () {try {for (INT I = 0; I <5; ++ I) {Wait (1); print_debug (I) ;}} catch (boost: thread_interrupted &) {print_debug ("thread_interrupted") ;}} void test_thread_wait1 () {boost: thread t (& threadfun1); // The join () method is a blocking call: It can suspend the current thread until the thread that calls join (). The operation is completed. T. join ();} void test_thread_wait2 () {boost: thread t (& threadfun2); wait (3); T. interrupt (); T. join ();} void test_thread_wait3 () {boost: thread t (& threadfun2); // The timed_join () method is also a blocking call: It can suspend the current thread, // wait until the running of the thread that calls join () ends or times out. timed_join (boost: posix_time: seconds (3);} void test_thread_wait4 () {boost: thread t (& threadfun2); wait (3 ); // when the thread is separated from the thread execution body, the thread execution body will continue without being affected, // until the running ends or ends with the main thread. T. detach (); // join does not work at this time t. join (); // t no longer identifies any thread {not-any-thread} assert (T. get_id () = boost: thread: ID ());}