#include <thread>
1 detach
脱离当前主线程,自由执行,乱序;
2 join()
等待模式,执行完再执行下一个
3 std::this_thread::get_id()
获取当前线程编号
4 std::thread::hardware_concurrency()
检测CPU有多少个核心
1 detach
脱离当前主线程,自由执行,乱序;
2 join()
等待模式,执行完再执行下一个
#include <iostream>
#include <thread> void run(int num)
{
std::cout << "hello world" << num << std::endl;
} void main()
{
std::thread *p[]; for (int i = ; i < ; i++)
{
p[i] = new std::thread(run, i);//循环创建线程
//p[i]->join();//等待模式,执行完再执行下一个
p[i]->detach();//脱离当前主线程,自由执行,乱序;
} system("pause");
}
1 join()
等待模式,执行完再执行下一个
2 std::this_thread::get_id()
获取当前线程编号
3 std::thread::hardware_concurrency()
检测CPU有多少个核心
#include <iostream>
#include <thread>
#include <windows.h> void msg()
{
MessageBoxA(, "对话框内容", "对话框标题", );//弹出对话框
} void main()
{
auto n = std::thread::hardware_concurrency();//检测CPU有多少个核心
std::cout << n << std::endl; std::cout << "thread=" << std::this_thread::get_id() << std::endl;//获取当前线程编号 std::thread thread1(msg);//创建多线程
std::thread thread2(msg); thread1.join();//开始执行,同时弹出2个对话框
thread2.join(); system("pause");
}
std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread *
threads.push_back(new std::thread(msg));//创建线程,并添加到数组
#include <iostream>
#include <thread>
#include <vector>
#include <windows.h> void msg()
{
MessageBoxA(, "对话框内容", "对话框标题", );//弹出对话框
} void main()
{
std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread * for (int i = ; i < ; i++)
{
threads.push_back(new std::thread(msg));//创建线程,并添加到数组
} for (auto th : threads)//遍历数组
{
th->join();//执行数组中的线程
} system("pause");
}
threads.push_back(new std::thread(msgA, i));//创建线程,并添加到数组,传入参数i,进行通信
#include <iostream>
#include <thread>
#include <vector>
#include <windows.h> void msgA(int num)
{
std::cout << std::this_thread::get_id() << " num=" << num << std::endl;//获取当前线程编号
MessageBoxA(, "对话框内容", "对话框标题", );//弹出对话框
} void main()
{
std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread * for (int i = ; i < ; i++)
{
threads.push_back(new std::thread(msgA, i));//创建线程,并添加到数组,传入参数i,进行通信
} for (auto th : threads)//遍历数组
{
th->join();//执行数组中的线程
} system("pause");
}
#include <thread>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- 在mysql 中两种锁定问题
mysql 中15.2.10.5 中描述了两个问题,且分别给出了解决办法. 1.向子表中写入数据,但写入之前需确保父表中存在其相应信息. 可能出现,在已经读取父表中的数据,但另一请求将其删除. 办法: ...
- Almost Prime
Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- WIFI无线adb调试android
有个需求需要支持android插上键盘,鼠标等外设,但是这样就不能使用microusb口进行adb调试了. 研究了一番,发现可以利用wifi进行adb无线调试(adb应该本身已经支持无线调试). WI ...
- 【翻译】MVC Music Store 教程-概述(二)
1. 文件->新建项目 软件安装 此篇将从运用免费的Visual Web Developer 2010Express来创建ASP.NET MVC3开始,逐步的添加一些功能来创建一个完整的应用程序 ...
- 限制TextBox输入,只能输入整数
public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...
- 【Lucene】挖掘相关搜索词
搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索.一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表.另一种方法 ...
- mysql中timestamp,datetime,int类型的区别与优劣
转载请注明来自 souldak,微博: @evagle 以下内容 整合筛选自互联网: int 1. 占用4个字节 2. 建立索引之后,查询速度快 3. 条件范围搜索可以使用使用between 4. 不 ...
- poj3006---素数筛法
#include <stdio.h> #include <stdlib.h> ];//以后都用宏定义 MAX int main()//如要将包括1000000在内的打表,数组就 ...
- usb键鼠标驱动分析
一.鼠标 linux下的usb鼠标驱动在/drivers/hid/usbhid/usbmouse.c中实现 1.加载初始化过程 1.1模块入口 module_init(usb_mouse_init); ...
- 怎样调通微信支付及微信发货通知接口(Js API)
怎样调通微信支付及微信发货通知接口(Js API) 微信支付提供了一个支付測试页面,微信支付正式使用须要測通支付.发货通知接口 .告警接口.维权接口.告警接口.维权接口非常easy.支付界面调通也相对 ...