[c++11 concurrent Programming] 02-join

Source: Internet
Author: User

1. Waiting for thread to finish

Without waiting for the thread to complete, we need to make sure that the data that the thread accesses is valid until the thread finishes. For example, the thread function holds a pointer or reference to a local variable, and when the function exits, the thread has not finished executing.

#include <thread> #include <iostream>//thread holds pointer to local variable struct func{int *i;func (int *i_): I (i_) {}void operator () () {for (unsigned j = 0; J < 100000; ++j) {*i = J;//access illegal address}}};//do not wait for thread execution to complete before exiting void oops () {int some_local_state = 0;func My_func (&some_local_state); Std::thread my_thread (My_func); My_thread.detach ();} int _tmain (int argc, _tchar* argv[]) {oops (); return 0;}

To avoid this, you need to replace the call of My_thread.detach () with the join () of the Std::thread instance, which guarantees that the thread has ended before the function exits. A join () can only be called once for a given thread, and once a join () is called, this Std::thread object is no longer connected and will return FALSE if its joinable () is called.

2. Waiting in an abnormal environment

We need to call the join or Detach method before the thread object is destroyed, and if you want to detach, call the detach method immediately after the thread is started. If you intend to wait for the thread, you need to carefully choose where to call join. If an exception occurs before a join is called after the thread has started, a call to the join may be skipped.

To prevent the application from being terminated when an exception is thrown, you need to call join when you have an exception.

void Do_something_in_current_thread () {throw ("error");} Exit void oops () {int some_local_state = 0;func My_func (&some_local_state) without waiting for thread execution to complete; Std::thread My_thread (My_func ); Try{do_something_in_current_thread ();} catch (const char *err_msg) {my_thread.join (); throw;} My_thread.join ();}
The Try/catch block ensures that the thread's join method is called regardless of whether the function exits gracefully or unexpectedly, but the Try/catch block looks verbose and can easily lead to confusion, and an easier way to use Raii-resource acquisition is Initialization and provides a class that calls join () in the destructor:

Class thread_guard{std::thread& t;public:explicit Thread_guard (std::thread& t_): T (t_) {}// The destructor checks whether the thread has not yet been join, and if not, calls ~thread_guard () {if (t.joinable ()) {T.join ()}} Marking a post-copy assignment operator as =delete to prevent the compiler from automatically generating, copying, or assigning a value such an object can be dangerous because it may exist longer than the scope of the thread it is bound to. Thread_guard (thread_guard const&) = delete;thread_guard& operator= (thread_guard const&) = delete;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[c++11 concurrent Programming] 02-join

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.