C++11并发之std::thread
- id
- Thread id (public member type ) id
- native_handle_type
- Native handle type (public member type )
成员函数:
- (constructor)
- Construct thread (public member function ) 构造函数
- (destructor)
- Thread destructor (public member function ) 析构函数
- operator=
- Move-assign thread (public member function ) 赋值重载
- get_id
- Get thread id (public member function ) 获取线程id
- joinable
- Check if joinable (public member function ) 判断线程是否可以加入等待
- join
- Join thread (public member function ) 加入等待
- detach
- Detach thread (public member function ) 分离线程
- swap
- Swap threads (public member function ) 线程交换
- native_handle
- Get native handle (public member function ) 获取线程句柄
- hardware_concurrency [static]
- Detect hardware concurrency (public static member function ) 检测硬件并发特性
Non-member overloads:
- swap (thread)
- Swap threads (function )
- default (1)
- thread() noexcept;
- initialization(2)
- template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
- copy [deleted] (3)
- thread (const thread&) = delete;
- move [4]
- hread (thread&& x) noexcept;
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<chrono>
- using namespace std;
- void fun1(int n) //初始化构造函数
- {
- cout << "Thread " << n << " executing\n";
- n += 10;
- this_thread::sleep_for(chrono::milliseconds(10));
- }
- void fun2(int & n) //拷贝构造函数
- {
- cout << "Thread " << n << " executing\n";
- n += 20;
- this_thread::sleep_for(chrono::milliseconds(10));
- }
- int main()
- {
- int n = 0;
- thread t1; //t1不是一个thread
- thread t2(fun1, n + 1); //按照值传递
- t2.join();
- cout << "n=" << n << '\n';
- n = 10;
- thread t3(fun2, ref(n)); //引用
- thread t4(move(t3)); //t4执行t3,t3不是thread
- t4.join();
- cout << "n=" << n << '\n';
- return 0;
- }
- 运行结果:
- Thread 1 executing
- n=0
- Thread 10 executing
- n=30</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- void show()
- {
- cout << "hello cplusplus!" << endl;
- }
- int main()
- {
- //栈上
- thread t1(show); //根据函数初始化执行
- thread t2(show);
- thread t3(show);
- //线程数组
- thread th[3]{thread(show), thread(show), thread(show)};
- //堆上
- thread *pt1(new thread(show));
- thread *pt2(new thread(show));
- thread *pt3(new thread(show));
- //线程指针数组
- thread *pth(new thread[3]{thread(show), thread(show), thread(show)});
- return 0;
- }</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- void show(const char *str, const int id)
- {
- cout << "线程 " << id + 1 << " :" << str << endl;
- }
- int main()
- {
- thread t1(show, "hello cplusplus!", 0);
- thread t2(show, "你好,C++!", 1);
- thread t3(show, "hello!", 2);
- return 0;
- }
- 运行结果:
- 线程 1线程 2 :你好,C++!线程 3 :hello!
- :hello cplusplus!</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<array>
- using namespace std;
- void show()
- {
- cout << "hello cplusplus!" << endl;
- }
- int main()
- {
- array<thread, 3> threads = { thread(show), thread(show), thread(show) };
- for (int i = 0; i < 3; i++)
- {
- cout << threads[i].joinable() << endl;//判断线程是否可以join
- threads[i].join();//主线程等待当前线程执行完成再退出
- }
- return 0;
- }
- 运行结果:
- hello cplusplus!
- hello cplusplus!
- 1
- hello cplusplus!
- 1
- 1</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- void show()
- {
- cout << "hello cplusplus!" << endl;
- }
- int main()
- {
- thread th(show);
- //th.join();
- th.detach();//脱离主线程的绑定,主线程挂了,子线程不报错,子线程执行完自动退出。
- //detach以后,子线程会成为孤儿线程,线程之间将无法通信。
- cout << th.joinable() << endl;
- return 0;
- }
- 运行结果:
- hello cplusplus!
- 0</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- int main()
- {
- auto n = thread::hardware_concurrency();//获取cpu核心个数
- cout << n << endl;
- return 0;
- }
- 运行结果:
- 8</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- const int N = 100000000;
- int num = 0;
- void run()
- {
- for (int i = 0; i < N; i++)
- {
- num++;
- }
- }
- int main()
- {
- clock_t start = clock();
- thread t1(run);
- thread t2(run);
- t1.join();
- t2.join();
- clock_t end = clock();
- cout << "num=" << num << ",用时 " << end - start << " ms" << endl;
- return 0;
- }
- 运行结果:
- num=143653419,用时 730 ms</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<mutex>
- using namespace std;
- const int N = 100000000;
- int num(0);
- mutex m;
- void run()
- {
- for (int i = 0; i < N; i++)
- {
- m.lock();
- num++;
- m.unlock();
- }
- }
- int main()
- {
- clock_t start = clock();
- thread t1(run);
- thread t2(run);
- t1.join();
- t2.join();
- clock_t end = clock();
- cout << "num=" << num << ",用时 " << end - start << " ms" << endl;
- return 0;
- }
- 运行结果:
- num=200000000,用时 128323 ms</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<atomic>
- using namespace std;
- const int N = 100000000;
- atomic_int num{ 0 };//不会发生线程冲突,线程安全
- void run()
- {
- for (int i = 0; i < N; i++)
- {
- num++;
- }
- }
- int main()
- {
- clock_t start = clock();
- thread t1(run);
- thread t2(run);
- t1.join();
- t2.join();
- clock_t end = clock();
- cout << "num=" << num << ",用时 " << end - start << " ms" << endl;
- return 0;
- }
- 运行结果:
- num=200000000,用时 29732 ms</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- const int N = 100000000;
- int num = 0;
- void run()
- {
- for (int i = 0; i < N; i++)
- {
- num++;
- }
- }
- int main()
- {
- clock_t start = clock();
- thread t1(run);
- t1.join();
- thread t2(run);
- t2.join();
- clock_t end = clock();
- cout << "num=" << num << ",用时 " << end - start << " ms" << endl;
- return 0;
- }
- 运行结果:
- num=200000000,用时 626 ms</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- int main()
- {
- auto fun = [](const char *str) {cout << str << endl; };
- thread t1(fun, "hello world!");
- thread t2(fun, "hello beijing!");
- return 0;
- }
- 运行结果:
- hello world!
- hello beijing!</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<chrono>
- using namespace std;
- int main()
- {
- thread th1([]()
- {
- //让线程等待3秒
- this_thread::sleep_for(chrono::seconds(3));
- //让cpu执行其他空闲的线程
- this_thread::yield();
- //线程id
- cout << this_thread::get_id() << endl;
- });
- return 0;
- }</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- class MyThread :public thread //继承thread
- {
- public:
- //子类MyThread()继承thread()的构造函数
- MyThread() : thread()
- {
- }
- //MyThread()初始化构造函数
- template<typename T, typename...Args>
- MyThread(T&&func, Args&&...args) : thread(forward<T>(func), forward<Args>(args)...)
- {
- }
- void showcmd(const char *str) //运行system
- {
- system(str);
- }
- };
- int main()
- {
- MyThread th1([]()
- {
- cout << "hello" << endl;
- });
- th1.showcmd("calc"); //运行calc
- //lambda
- MyThread th2([](const char * str)
- {
- cout << "hello" << str << endl;
- }, " this is MyThread");
- th2.showcmd("notepad");//运行notepad
- return 0;
- }
- 运行结果:
- hello
- //运行calc
- hello this is MyThread
- //运行notepad</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- #include<cstdarg>
- using namespace std;
- int show(const char *fun, ...)
- {
- va_list ap;//指针
- va_start(ap, fun);//开始
- vprintf(fun, ap);//调用
- va_end(ap);
- return 0;
- }
- int main()
- {
- thread t1(show, "%s %d %c %f", "hello world!", 100, 'A', 3.14159);
- return 0;
- }
- 运行结果:
- hello world! 100 A 3.14159</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- int main()
- {
- thread t1([]()
- {
- cout << "thread1" << endl;
- });
- thread t2([]()
- {
- cout << "thread2" << endl;
- });
- cout << "thread1' id is " << t1.get_id() << endl;
- cout << "thread2' id is " << t2.get_id() << endl;
- cout << "swap after:" << endl;
- swap(t1, t2);//线程交换
- cout << "thread1' id is " << t1.get_id() << endl;
- cout << "thread2' id is " << t2.get_id() << endl;
- return 0;
- }
- 运行结果:
- thread1
- thread2
- thread1' id is 4836
- thread2' id is 4724
- swap after:
- thread1' id is 4724
- thread2' id is 4836</span>
- <span style="font-size:12px;">#include<iostream>
- #include<thread>
- using namespace std;
- int main()
- {
- thread t1([]()
- {
- cout << "thread1" << endl;
- });
- cout << "thread1' id is " << t1.get_id() << endl;
- thread t2 = move(t1);;
- cout << "thread2' id is " << t2.get_id() << endl;
- return 0;
- }
- 运行结果:
- thread1
- thread1' id is 5620
- thread2' id is 5620</span>
转载于:https://www.cnblogs.com/mmc9527/p/10427924.html
C++11并发之std::thread相关推荐
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- C++11 多线程(std::thread)详解
注:此教程以 Visual Studio 2019 Version 16.10.3 (MSVC 19.29.30038.1) 为标准,大多数内容参照cplusplus.com里的解释 此文章允许转载, ...
- std::thread使用
c++ 11 之后有了标准的线程库:std::thread.通过c++11中的线程库创建线程,极为方便,且跨平台,是语言层面的.之前跨平台的多线程开发中,多使用boost 相关第三方库. 1.std: ...
- C++11中头文件thread的使用
C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类.C++11的标准类std::thread对线程进行了封装.std::thread代表了一个线程对象 ...
- C++多线程编程实战01:std::thread
C++多线程:std::thread 文章目录 C++多线程:std::thread 定义 构造函数 析构函数 赋值操作函数 join与datch 例子 例子 其它 基本用法 线程参数 等待线程完成( ...
- std::thread与pthread
线程std::thread与pthread对比 多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序.一般情况下,两种类型的多任务处理:基于进程和基于线程. 基于进程的多 ...
- C++多线程:std::thread
最近这段时间在学习C++多线程相关的知识,打算将学习的内容记录下来,加深理解和记忆. C++11 新标准中引入了五个头文件来支持多线程编程,他们分别是<atomic> ,<threa ...
- C++11 新特性之std::thread
C++11 新特性之std::thread 原文:https://blog.csdn.net/oyoung_2012/article/details/78958274 从C++11开始,C++标准库已 ...
- 【多线程】C++11进行多线程开发 (std::thread)
文章目录 创建线程 std::thread 类 使用join() 使用 detach() 警惕作用域 线程不能复制 给线程传参 传递指针 传递引用 以类成员函数为线程函数 以容器存放线程对象 互斥量 ...
最新文章
- 教AI区分因果关系和相关性,将改变下一代 AI 的研发
- 也谈淘点点60s短信订单的架构设计
- Java黑皮书课后题第8章:*8.30(代数:解答线性方程)编写一个方法,解答下面的2*2线性方程组系统
- Java8新特性之Lambda
- php为什么凉了_马蜂窝裁php换java,php又又又凉凉了吗
- Wi-Fi 6还没用上,Wi-Fi 7就要来了?
- 解决Conda install tensorflow弹窗Python.exe已经停止工作的问题
- 获取北京时间 日开始时间戳
- Win7 安装 -- SHSUCDX can’t install 问题解决
- android 当手柄打游戏,玩手游需不需要配手柄?谈移动游戏外设的三个机会
- 车间和仓库可以一起吗_为什么厂房和仓库不能混用?
- 女篮亚军,为啥男篮那么水?
- [Unity3D] Unity3D连接安卓设备调试unity程序
- 【leetcode】 剑指 Offer学习计划(java版本含注释)(上)
- 统计学怎么求加权指数_暨南大学《统计学》中文习题 第十二章 统计指数
- 简单了解软件开发的生命周期和流程、思想
- 手机测试android程序下载,测手速下载_测手速手机版下载「安卓版」-太平洋下载中心...
- Parallelism , Partitioner
- 我眼中的互联网运营感观——运营是什么?
- 有趣的智能图像处理demo汇总
热门文章
- CSS 相对|绝对(relative/absolute)定位系列(二)
- CSS之background-size属性
- grid比flex更强大的属性
- std::function和std::bind
- c++ fork 进程时 共享内存_c/c++ Linux 进程间通信------共享内存
- css ——行级元素与块级元素解析
- openstack placement 组件作用理解
- mysql批量生成修改表和列注释语句
- 用nodejs 替换文件中所有图片的url
- 基于visual Studio2013解决C语言竞赛题之0304整除数