C++11多线程(二):std:thread
2015-12-22 22:03
549 查看
参考链接:http://www.cnblogs.com/haippy/p/3236136.html
目录
1.传参
2.move构造
3.其他成员函数
1.传参
2.move
move: thread& operator= (thread&& rhs) noexcept;
move 构造函数,move 构造函数,调用成功之后 rhs不代表任何 thread 执行对象。
3.其他成员函数
get_id
获取线程 ID。
joinable
检查线程是否可被 join。
join
阻塞主线程,等待子线程结束。当该线程执行完成后才返回。(即等待子线程执行完毕才继续执行主线程)
detach
Detach 线程。当线程主函数执行完之后,线程就结束了,运行时库负责清理与该线程相关的资源。一般join与detach二选一。
swap
Swap 线程 。
native_handle
返回 native handle。
hardware_concurrency [static]
检测硬件并发特性。
目录
1.传参
2.move构造
3.其他成员函数
1.传参
#include "stdafx.h" #include <iostream> using namespace std; #include <thread> #include <vector> #include <mutex> void func1(int num) { cout << "--------------" << endl; cout << "thread id " << this_thread::get_id() << endl; for (size_t i = 0; i < 5; i++) { cout << num++ << endl; this_thread::sleep_for(chrono::milliseconds(10)); } } void func2(int &num) { cout << "--------------" << endl; cout << "thread id " << this_thread::get_id() << endl; for (size_t i = 0; i < 5; i++) { cout << num++ << endl; this_thread::sleep_for(chrono::milliseconds(10)); } } int _tmain(int argc, _TCHAR* argv[]) { int num = 0; thread thread1(func1, num); thread1.join(); cout << "----" << endl; cout << "after thread1 , num:"<<num << endl; thread thread2(func2, ref(num)); thread2.join(); cout << "----" << endl; cout << "after thread2 , num:" << num << endl; cout << "-----end-------" << endl; return 0; }输出:
-------------- thread id 7484 0 1 2 3 4 ---- after thread1 , num:0 -------------- thread id 7404 0 1 2 3 4 ---- after thread2 , num:5 -----end------- 请按任意键继续. . .
2.move
move: thread& operator= (thread&& rhs) noexcept;
move 构造函数,move 构造函数,调用成功之后 rhs不代表任何 thread 执行对象。
void func2(int &num) { cout << "--------------" << endl; cout << "thread id " << this_thread::get_id() << endl; for (size_t i = 0; i < 5; i++) { cout << num++ << endl; this_thread::sleep_for(chrono::milliseconds(10)); } } int _tmain(int argc, _TCHAR* argv[]) { int num = 0; thread thread2(func2, ref(num)); thread thread3(move(thread2));//thread3会执行func2,thread2不再是线程 thread3.join(); cout <<"num:" <<num << endl; cout << "-----end-------" << endl; return 0; }输出:
-------------- thread id 8760 0 1 2 3 4 num:5 -----end------- 请按任意键继续. . .
3.其他成员函数
get_id
获取线程 ID。
joinable
检查线程是否可被 join。
join
阻塞主线程,等待子线程结束。当该线程执行完成后才返回。(即等待子线程执行完毕才继续执行主线程)
detach
Detach 线程。当线程主函数执行完之后,线程就结束了,运行时库负责清理与该线程相关的资源。一般join与detach二选一。
swap
Swap 线程 。
native_handle
返回 native handle。
hardware_concurrency [static]
检测硬件并发特性。
相关文章推荐
- 黑马程序员--C语言自学笔记---12fgets、fputs、const、结构体
- C++二叉树的构建及求深度,叶子数量,层次遍历
- C++ vector容器的4种遍历方式
- 森浦资讯(C++ 面试两道程序题)
- 【c/c++】指针函数和函数指针
- C语言各种数据类型取值范围
- C语言 第一章
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- 设计模式系列(十四)组合模式(Composite Pattern)
- c++获取cpu信息代码
- C++11 并发指南二(std::thread 详解)
- C++ typedef用法小结 (※不能不看※)
- C++11 并发指南一(C++11 多线程初探)
- C语言的基础
- WINDOWS7下VC++ 6.0 无法打开文件(打开文件时软件崩溃)的解决办法。
- vc++笔记十一
- 排序(6)---------归并排序(C语言实现)
- 【c/c++】指针数组和数组指针
- n皇后(C++)