C++11 multithreaded learning Note one thread base uses

Source: Internet
Author: User

There's nothing to talk about. Basic use of the C++11 thread class

#include "stdafx.h" #include <iostream> #include <thread>void Func (int n) {for (int i = 0; i < 3; ++i) Std::cou T << "func" << i << Std::endl;} void Reffunc (int& N) {for (int i = 0; i < 3; ++i) std::cout << "ref func" << i << Std::endl;} int _tmain (int argc, _tchar* argv[]) {int n = 0;std::thread t1;//No display std::thread T2 (Func, n + 1);//functions that call non-reference parameters T2.join (); STD :: Thread t3 (Reffunc, Std::ref (n));//function calling reference parameter Std::thread T4 (Std::move (T3)); T4.join (); return 0;}

The code shows several uses of the thread class. Transfer function parameters, with reference and without reference two kinds.

where T3 uses move to associate threads to T4

T3 is not associated with a thread. The move involved is seen later in this article

Shown below:

Func 0
Func 1
Func 2
Ref Func 0
Ref Func 1
Ref Func 2
Please press any key to continue ...

There is also the use of join. If the code of the join IS advanced, the information of two threads is out of order

int _tmain (int argc, _tchar* argv[]) {int n = 0;std::thread t1;//No display std::thread T2 (Func, n + 1);//function calling non-reference parameter Std::thread t 3 (Reffunc, Std::ref (n));//The function that invokes the reference parameter Std::thread t4 (Std::move (T3)); T2.join (); T4.join (); return 0;}

  

Shown below:

Func 0ref func 0

Func 1
Func 2
Ref Func 1
Ref Func 2
Please press any key to continue ...

C++11 multithreaded learning Note one thread base uses

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.