std::thread 五:打包任务(packaged_task)
#include <iostream>
#include <thread>
#include <mutex>
#include <list>
#include <future>
using namespace std; int myThread(int num)
{
cout << "myThread() start thread id =" << this_thread::get_id() << endl;
cout << "num is " << num << endl;
std::chrono::microseconds s(5000);
std::this_thread::sleep_for(s);
cout << "myThread() end thread id =" << this_thread::get_id() << endl;
return 5;
} int main()
{
cout << "main run thread id =" << std::this_thread::get_id() << endl;
/* packaged_task 包装函数,执行线程,可以拿到线程的返回值,不知道工作中应用场景在哪,但是学都学了,得记下来 */
// 创建一个打包任务,把任务包装起来
std::packaged_task<int(int)> mypt(myThread);
std::thread t(std::ref(mypt), 121212);
t.join();
std::future<int> res = mypt.get_future();
cout << res.get() << endl;
cout << "main end" << endl;
return 0;
}
std::thread 五:打包任务(packaged_task)的更多相关文章
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- C++ std::thread
std::thread Defined in header class thread The class thread represents a single thread of execution. ...
- std::thread线程库详解(3)
目录 目录 前言 lock_guard scoped_lock (C++17) unique_lock shared_lock 总结 ref 前言 前两篇的博文分别介绍了标准库里面的线程和锁,这一次的 ...
- 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 ...
- 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对象创建时启动.最简单的情况下, ...
随机推荐
- matrox的RAP4G4C12 CXP采集卡软件安装
Hello-FPGA info@hello-fpga.cOM matrox的RAP4G4C12 CXP采集卡软件安装 目录 matrox的RAP4G4C12 CXP采集卡软件安装 4 1 前言 4 2 ...
- 【Azure Function App】Java Function在运行中遇见内存不足的错误
问题描述 在Function的Code+Test界面进行函数触发可以成功.因为Function为Blob Trigger,当在Blob容器下上传文件后,Function可以被正常触发但是报 outof ...
- 【Azure Developer】使用MSAL4J 与 ADAL4J 的SDK时候,遇见了类型冲突问题 "java.util.Collections$SingletonList cannot be cast to java.lang.String"
问题描述 在博文 "[Azure Developer]使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码 ...
- spring重点后置处理器
1. DefaultListableBeanFactory的作用: 默认实现了ListableBeanFactory和BeanDefinitionRegistry接口,基于bean definitio ...
- React函数式组件使用@emotion时一定要注意的问题!
怎么说呢,一个坑,踩了两天,总觉得是useSate和input的传值方法问题 在useMemo和useCallback反复测试问题 最后没办法,通过最傻方式,一点点注释代码,发现了问题 const C ...
- Zabbix技术分享——snmp异常排查指南
大家好,我是乐乐.在IT运维中,难免会碰上设备snmp不通的情况,那么,当问题出现的时候,运维工程师该如何快速找到问题所在呢?下面让我们一起来看看吧! 1.IP配置检查 首先检查zabbix监控上 ...
- python 文件操作常用方法
一 python文件创建 import os import argparse def file_write(file_name,msg): f = open(file_name,"a&quo ...
- day03-2-应用线程02
JavaGUI-坦克大战03-2 7.线程的应用02 7.3.坦克大战4.0版 坦克大战4.0版 增加功能: 功能1.让敌人的坦克也能够发射子弹(可以有多个子弹) 功能2.当我方坦克集中敌人坦克时,敌 ...
- EL表达式 参考手册
一.EL简介 1.语法结构 ${expression} 2.[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或 ...
- do{}while(0)用法
更安全的展开宏定义 #define DOSOMETHING foo1(); foo2(); if(condition) DOSOMETHING /* if(condition) foo1(); foo ...