C ++ parallel programming 2

Source: Internet
Author: User

C ++ parallel programming 2
Start the thread to view the thread constructor and accept a function whose return value is void. As follows: void do_some_work (); std: thread my_thread (do_some_work); you can also accept a class that redefines the operator, as follows: class background_task {public: void operator ()() const {do_something (); do_something_else () ;}}; background_task f; std: thread my_thread (f); however, it is incorrect to use the following code as a function: std: thread my_thread (background_task (); you can use: std: thread my_thread (background_task (); std: thread my_thread {background_task ()}; an example of full startup is: # include <thre Ad> void do_something (int & I) {++ I;} struct func {int & I; func (int & I _): I (I _) {} void operator () {for (unsigned j = 0; j <1000000; ++ j) {do_something (I) ;}}; void oops () {int some_local_state = 0; func my_func (some_local_state); std: thread my_thread (my_func); my_thread.detach ();} int main () {oops ();} as long as the executable code is passed in during thread construction, the execution starts, but the detach is called, so that the main thread will not wait for the sub-thread, and the sub-thread shares the variable some_local_state, this will lead to illegal access. Therefore, you need to wait for the sub-thread. The waiting thread my_thread.detach () should be changed to my_thread.join (); join () can only be called once. If yes, joinable () will return false. Exception Handling if the main thread is still doing something, it is possible to exit unexpectedly. In this case, we still have to handle exceptions without waiting for the subthread. There are two methods: try {do_something_in_current_thread ();} catch (...) {if (my_thread.joinable () {my_thread.join ();} throw;} or class thread_guard {std: thread & t; public: explicit thread_guard (std: thread & t _): t (t _){}~ Thread_guard () {if (t. joinable () {t. join () ;}} thread_guard (thread_guard const &) = delete; thread_guard & operator = (thread_guard const &) = delete ;}; thread_guard g (my_thread); do_something_in_current_thread (); the background running thread calls detach (). the application scenario is, for example, word editing. When a new document is created, a thread is started. There is no need to wait between the two documents so that the background will run the thread.

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.