Manage Thread Creation Threads

Source: Internet
Author: User

Basic thread management includes: 1, creating threads. 2. Wait for it to end or run in the background. 3. Pass parameters to the thread function and take ownership of the threaded object. 4. Select threads and use specific threads.

Creating ThreadsThe thread starts running when the thread object is created, and the thread object is created using Std::thread. Like the last function in the previous section, the simplest case is to run a function with no return value and no parameters after the thread object is created. This function runs in the thread until it returns, at which point it terminates. In the most complex case, the function that the thread runs may be a function object that requires passing parameters and performing a series of independent operations that require some information from the system until a signal is terminated. It doesn't matter what the thread does or where it starts, the first way to start a thread is to create a thread object Std::thread objects:
void Do_some_work (); Std::thread my_thread (do_some_work);</span>
This is the simplest case, std::thread can also use the callable type (callable type), where you can pass an instance of an object at the calling function when you create the thread object.
Class background_task{public:void operator () () const{do_something ();d o_something_else ();}}; Background_task F;std::thread My_thread (f);
In this case, when the new thread is created, the function objectis copied to the new thread.In memory, and at this start call. You must ensure that the copy behavior is equal to the original object, otherwise you will not get the expected results.avoid being called "C + + 's most annoying syntax" When passing function objects to a thread constructor. If you pass a temporary object instead of a named variable, the syntax is exactly the same as a function declaration, and the compiler takes it as a function declaration, not as an object definition. For example:
Std::thread My_thread (Background_task ());
Declares a function my_thread, which has a parameter (pointer to a function, returns a Background_task object), returns a Std::thread object instead of creating a new thread. There are 3 ways to avoid this: 1 Name the function object as above. 2 use extra parentheses. 3. Use the new standard initialization syntax.the 2nd method
Std::thread My_thread ((Background_task ()));
The addition of parentheses prevents the compiler from interpreting it as a function declaration, so that my_thread can be interpreted as defining the Std::thread type object.the 3rd method
Std::thread my_thread{background_task ()};
Use the new standard initialization syntax, with curly braces instead of parentheses.One type: Callable objects (callable object) to avoid the problem above is the use of lambda expressions (lambda expression). This is a new feature of C++11, which allows you to use a reject function to catch local variables and avoid passing in parameters. The above example can be written
Std::thread My_thread ([] (do_something ();d o_something_else ();});

Once you start your thread, you must show the decision whether to wait for it to end (call join) or allow it to be allowed independently (call detach). If you do not decide before the thread object is destroyed, your thread terminates (Std:thread's destructor call Std::terminate ()). It is important to ensure that your thread is properly terminated or detached, even if it is sent in an abnormal situation. Note that you want to make a decision before the thread object is refactored--when you make a decision, the threads may already be terminated or detached, and if you detach it, it may also run after a thread object is refactored. If you do not wait for the thread to end, make sure that the data used by the thread is valid until the thread ends. This is not a new issue, even if the single-threaded code page has such a problem. (in Linux multi-threaded server programming, you have talked about this problem, using smart pointers). One scenario is that the function uses a pointer or reference to a local object, and the thread does not terminate when the function terminates. Let's look at this example:
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 ( );}
In this example, the new thread may run (detach the new thread) after the Oops function terminates, and if the thread is still running, the function do_something (i) uses the variable that has already been destroyed.a common way to avoid the above problem is to make the thread function independent and copy the data to the thread rather than to the share. If you use a callable type object (Callable objects), the object is copied into the object and the original object is immediately destroyed. However, at this point you should also note whether the object contains pointers or references. Using local variables within a thread function is a bad idea unless you can ensure that the function is always there to know that the thread is executing (calling the join waiting thread within the function).

Manage Thread Creation Threads

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.