std::thread(2)
个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子演示如何使用这个 id:
#include <thread>
#include <iostream>
#include <vector> void hello(){
std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
} int main(){
std::vector<std::thread> threads; for(int i = ; i < ; ++i){
threads.push_back(std::thread(hello));
} for(auto& thread : threads){
thread.join();
} return ;
}
依次启动每个线程,然后把它们保存到一个 vector 容器中,程序执行结果是不可预测的,例如: Hello from thread
Hello from thread
Hello from thread
Hello from thread
Hello from thread 也可能是: Hello from thread Hello from thread Hello from thread 139810974787328Hello from thread 139810983180032Hello from thread 或者其他结果,因为多个线程的执行是交错的。你完全没有办法去控制线程的执行顺序(否则那还要线程干吗?)
使用 lambda 启动线程
当线程要执行的代码就一点点,你没必要专门为之创建一个函数,你可以使用 lambda 来定义要执行的代码,因此第一个例子我们可以改写为:
#include <thread>
#include <iostream>
#include <vector> int main(){
std::vector<std::thread> threads; for(int i = ; i < ; ++i){
threads.push_back(std::thread([](){
std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}));
} for(auto& thread : threads){
thread.join();
} return ;
}
在这里我们使用了一个 lambda 表达式替换函数指针,而结果是一样的。
std::thread(2)的更多相关文章
- std::thread
std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...
- 用std::thread替换实现boost::thread_group
thread_group是boost库中的线程池类,内部使用的是boost::thread. 随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库 ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...
- C++11多线程std::thread的简单使用
在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...
- std::thread使用
本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...
- linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决
linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决 在linux在需要使用c++11时会遇到 ...
- Microsoft C++ 异常: std::system_error std::thread
第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...
随机推荐
- mosquitto $SYS下topic
$SYS/broker/clients/connected
- 转:Mosquitto用户认证配置
转自:https://blog.csdn.net/u012377333/article/details/69397124?utm_source=blogxgwz1 前言:基于Mosquitto服务器已 ...
- CTC loss 理解
参考文献 CTC学习笔记(一) 简介:https://blog.csdn.net/xmdxcsj/article/details/51763868 CTC学习笔记(二) 训练和公式推导 很详细的公示推 ...
- 百度MIP(百度版的google AMP)了解一下?
官网:https://www.mipengine.org/ 视频教学:http://bit.baidu.com/subject/datalist/sid/10/cid/22.html github:h ...
- WiX and System Folders 系统目录 installshield 如何将文件安装到C盘根目录
Property name Brief description of property AdminToolsFolder Full path to the directory containing a ...
- Atitit.数据库事务隔离级别 attilax 总结
Atitit.数据库事务隔离级别 1. 事务隔离级别的作用 1 2. 在的隔离级别 2 3. 常见数据库的默认管理级别 3 1. 事务隔离级别的作用 较低的隔离级别可以增强许多用户同时访问数据的能力, ...
- location if (.....) #if与中括号之间要有空格
[root@web01 default]# /app/server/nginx/sbin/nginx -t nginx: [emerg] unknown directive nginx: config ...
- 1、Reactive Extensions for .NET(译)
注:本文的工程是基于 vs2010 的,在 vs2012 中区别不大. 本文的意图是让读者熟悉 Reactive Extension for .net(Rx) 的使用.通过一系列的例子,让读者感受 基 ...
- Spring Cloud心跳监测
Spring Cloud实现心跳监测,在服务注册和停止时,注册中心能得到通知,并更新服务实例列表 Spring Cloud注册中心添加配置: eureka.server.enable-self-pre ...
- 实例37foreach遍历数组
package test; import java.util.List; import java.util.ArrayList; import java.util.Scanner; /** * @au ...