Concurrent Programming (2) thread management

Source: Internet
Author: User

First, start the thread

void do_something (); Std::thread t (do_something);

Note that T is the thread that is created, do_something is a function object, and do not pass in the function. The provided function objects are copied into the storage space of the new thread, and the execution and invocation of the function objects are performed in the memory space of the threads.

Join (), detach ()

Two, line path ginseng

Thread invocation pass-through parameter

void f (int i,std::string const& s); Std::thread t (f,3, "Hello");

Note that thread initialization does not implicitly convert misinform parameters, and requires an explicit conversion.

Iii. Transfer of thread ownership

Std::move () to transfer thread ownership, the source thread is not associated with the execution thread after the transfer.

void some_function (); void  //  1//  2//  3//  4// 5 // 6 The assignment will crash the program, and T1 already has a thread. 

Four, the runtime determines the number of threads

Std::thread::hardware_concurrency () This function returns the number of threads that can concurrently concurrent in a program. For example, in multicore systems, the return value can be the number of CPU cores. Because a context-sensitive switchover can degrade the performance of a thread, there are certainly fewer threads to start than the number of threads supported by the hardware.

Concurrent Programming (2) thread management

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: 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.