首页 > 其他分享 >并发多线程11std::atomic续谈、std::async深入谈

并发多线程11std::atomic续谈、std::async深入谈

时间:2022-09-01 15:01:33浏览次数:66  
标签:11std std 调用 launch 创建 线程 async 多线程

std::atomic续谈、std::async深入谈

一、std::atomic续谈

#include <iostream>
#include <thread>
#include <atomic>
using namespace std;
std::atomic<int> g_count = 0; //封装了一个类型为int的 对象(值)
 
void mythread1() {
    for (int i = 0; i < 1000000; i++) {
         //虽然g_count使用了原子操作模板,但是这种写法既读又写,
         //会导致计数错误
         g_count = g_count + 1;
    }
}

int main() {
    std::thread t1(mythread1);
    std::thread t2(mythread1);
    t1.join();
    t2.join();
    cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}
//结果不是2000000

一般atomic原子操作,针对++,--,+=,-=,&=,|=,^=是支持的,其他操作不一定支持。

二、std::async深入理解
2.1 std::async参数详述,async 用来创建一个异步任务

延迟调用参数 std::launch::deferred【延迟调用】,std::launch::async【强制创建一个线程】

std::async()我们一般不叫创建线程(他能够创建线程),我们一般叫它创建一个异步任务。

std::async和std::thread最明显的不同,就是 async 有时候并不创建新线程。

①如果用std::launch::deferred 来调用async?

延迟到调用 get() 或者 wait() 时执行,如果不调用就不会执行

②如果用std::launch::async来调用async?

强制这个异步任务在新线程上执行,这意味着,系统必须要创建出新线程来运行入口函数。

③如果同时用 std::launch::async | std::launch::deferred

这里这个 | 意味着async的行为可能是 std::launch::async 创建新线程立即执行, 也可能是 std::launch::deferred 没有创建新线程并且延迟到调用get()执行,由系统根据实际情况来决定采取哪种方案

④不带额外参数 std::async(mythread),只给async 一个入口函数名,此时的系统给的默认值是 std::launch::async | std::launch::deferred 和 ③ 一样,有系统自行决定异步还是同步运行。

2.2 std::async和std::thread()区别:

std::thread()如果系统资源紧张可能出现创建线程失败的情况,如果创建线程失败那么程序就可能崩溃,而且不容易拿到函数返回值(不是拿不到)
std::async()创建异步任务。可能创建线程也可能不创建线程,并且容易拿到线程入口函数的返回值;

由于系统资源限制:
①如果用std::thread创建的线程太多,则可能创建失败,系统报告异常,崩溃。

②如果用std::async,一般就不会报异常,因为如果系统资源紧张,无法创建新线程的时候,async不加额外参数的调用方式就不会创建新线程。而是在后续调用get()请求结果时执行在这个调用get()的线程上。

如果你强制async一定要创建新线程就要使用 std::launch::async 标记。承受的代价是,系统资源紧张时可能崩溃。

③根据经验,一个程序中线程数量 不宜超过100~200 。

2.3 async不确定性问题的解决
不加额外参数的async调用时让系统自行决定,是否创建新线程。

std::future<int> result = std::async(mythread);
问题焦点在于这个写法,任务到底有没有被推迟执行。

通过wait_for返回状态来判断:

std::future_status status = result.wait_for(std::chrono::seconds(6));
//std::future_status status = result.wait_for(6s);
    if (status == std::future_status::timeout) {
        //超时:表示线程还没有执行完
        cout << "超时了,线程还没有执行完" << endl;
    }
    else if (status == std::future_status::ready) {
        //表示线程成功放回
        cout << "线程执行成功,返回" << endl;
        cout << result.get() << endl;
    }
    else if (status == std::future_status::deferred) {
        cout << "线程延迟执行" << endl;
        cout << result.get() << endl;
    }

 


原文链接:https://blog.csdn.net/qq_38231713/article/details/106093332

 

标签:11std,std,调用,launch,创建,线程,async,多线程
From: https://www.cnblogs.com/gk520/p/16646512.html

相关文章

  • 并发多线程10 future其他成员函数、shared_future、atomic
    第十节future其他成员函数、shared_future、atomic一、std::future的成员函数1、std::future_statusstatus=result.wait_for(std::chrono::seconds(几秒));卡住当前......
  • Typora多线程批量上传图片,永久免费25G图床
    为了满足日常需求,就写了一个自动上传图片到图床的脚本运行该程序可以做到自动完成图片上传,并自动替换为网络链接,支持多图同时上传,采用了多线程,上传速度提升很明显。以Win......
  • 并发多线程8condition_variable、wait、notify_one、notify_all
    第八节condition_variable、wait、notify_one、notify_all一、条件变量condition_variable、wait、notify_one、notify_allstd::condition_variable实际上是一个类,是一个......
  • 并发多线程6
    第六节unique_lock(类模板)详解1.unique_lock取代lock_guardunique_lock比lock_guard灵活很多(多出来很多用法),效率差一点。unique_lock<mutex>myUniLock(myMutex);2.uni......
  • 多线程01
     一个进程可以有多个线程,至少有一个线程,比如视频中同时听声音,看图像真正的多线程是指有多个cpu,即多核,例如服务器。如果是模拟出来的多线程,在一个cpu的情况下,在同一个时......
  • 多线程之线程实现和状态
    多线程1.多线程Thread概述1.1线程简介多任务:同时做多件事情(一遍看电视一遍吃饭);不过,看起来是多个任务同时在做,其实本质上我们的大脑在同一时间依旧是只做了一件事(速......
  • Java8 多线程及并行计算demo
    Java8多线程及并行计算demo #接口publicinterfaceRemoteLoader{Stringload();defaultvoiddelay(){try{Thread.sleep(1000L......
  • 多线程实战双色球
    随机数索引生成代码: usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceYpDotNet......
  • 一次有趣的多线程调度问题
    线程1----wait()1Objecto=newObject();2waitAndNotifyMap.put(urlId,o);3System.out.println(urlId+"运行到阻塞");4synchronized(o){5o.wait(1000);6......
  • C++【多线程编程】之【线程安全】
    1.线程安全是什么?在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况。2.什么情况......