std::thread 四:异步(async)
#include <iostream>
#include <thread>
#include <mutex>
#include <list>
#include <future>
using namespace std; int myThread()
{
cout << "myThread() start thread id =" << this_thread::get_id() << endl;
std::chrono::microseconds s(5000);
std::this_thread::sleep_for(s);
cout << "myThread() end thread id =" << this_thread::get_id() << endl;
return 5;
} class A
{
public:
int myThread(int num)
{
cout << num << endl;
cout << "myThread() start thread id =" << this_thread::get_id() << endl;
std::chrono::microseconds s(5000);
std::this_thread::sleep_for(s);
cout << "myThread() end thread id =" << this_thread::get_id() << endl;
return 10;
}
}; int main()
{
cout << "main run thread id =" << std::this_thread::get_id() << endl;
// 创建一个异步线程,使用函数作为参数
std::future<int> res = std::async(myThread);
// 创建一个异步线程,使用类作为参数
A a;
std::future<int> res1 = std::async(&A::myThread, &a, 1234);
cout << "continue ....." << endl;
// 获取线程返回的值
cout << res.get() << endl;
cout << res1.get() << endl;
cout << "main end" << endl;
return 0;
}
std::thread 四:异步(async)的更多相关文章
- c++ 如何获取多线程的返回值?(std::thread ,std::async)
//简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...
- 同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式
1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端: 同步: ...
- C++11并发——多线程std::thread (一)
https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
- C++ std::thread
std::thread Defined in header class thread The class thread represents a single thread of execution. ...
- C++并发低级接口:std::thread和std::promise
std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...
- Python 进阶 异步async/await
一,前言 本文将会讲述Python 3.5之后出现的async/await的使用方法,我从上看到一篇不错的博客,自己对其进行了梳理.该文章原地址https://www.cnblogs.com/dhcn ...
- 关于std::thread
std::thread基本用法 1.普通函数: std::thread thread(func, param, ...) 2.类成员函数: std::thread thread(&class_ ...
- C++11并发之std::thread<转>
最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...
- C++11并发编程:多线程std::thread
一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...
- C++11多线程std::thread创建方式
//#include <cstdlib> //#include <cstdio> //#include <cstring> #include <string& ...
随机推荐
- VIM初使化
vim ~/.vimrc #设置编码 set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936 #显示行号 set number #一个tab为4个空格 ...
- 【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
问题描述 使用Flask框架部署Python代码,如何访问其中的静态文件呢?如static问价夹中的图像资源,同时如何在代码中读取txt文件中的内容呢?是相对路径或者是绝对路径呢? 实验步骤 在App ...
- SpringBoot2.x 启动过程详解
spring 简化了java应用开发, 而springboot则简化了 spring应用的开发,用约定优于配置优于编码的方式快速构建spring对其他框架的整合. 官方文档 探究Hello,World ...
- AtCoder Beginner Contest 338(A~E补题)
目录 A B C题 D题 E题 A 签到 #include <bits/stdc++.h> #define rep(i,a,b) for(int i = (a); i <= (b); ...
- RAPTOR 一种基于树的RAG方法,RAG的准确率提高 20%
一种理解整个文档上下文的新颖的 RAG 方法 RAG 是当前使用LLM的标准方法,大多数现有方法仅从检索语料库中检索短的连续块,限制了对整个文档上下文的整体理解. 最近,一种名为 RAPTOR (Re ...
- chatGPT初体验
chatGPT NLP技术,通过统计的手段模拟出更正确的答案. 他与以前的NLP不一样,他有上下文语义,他能够模拟场景,能够总结很多文章信息. 因此对于谷歌等搜索引擎就很有攻击性了,因为chatGPT ...
- Python文件操作系统
[一]文件操作基本流程 # 1. 打开文件,由应用程序向操作系统发起系统调用open(...),操作系统打开该文件,对应一块硬盘空间,并返回一个文件对象赋值给一个变量f f=open('a.txt', ...
- Temporary notepad
20230412 快速帮助手册 很牛的风格 https://devhints.io/vue 印记中国 https://docschina.org/ 前台本地存储数据 兼容 https://github ...
- Ayu vscode主题
Ayu vscode主题
- CMake的作用和价值--概念简介
一 简介: CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...