首页 > 其他分享 >std::thread 四:异步(async)

std::thread 四:异步(async)

时间:2023-06-18 23:22:38浏览次数:41  
标签:std 异步 cout thread myThread async include

 

*:如果 std::async 中传递参数 std::lunnch::deferred ,就需要等待调用 get() 或者 wait() 才会执行,并且代码非子线程运行,而是在主线程中执行

 

#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,异步,cout,thread,myThread,async,include
From: https://www.cnblogs.com/shiyixirui/p/17489990.html

相关文章

  • std::thread 五:打包任务(packaged_task)
     #include<iostream>#include<thread>#include<mutex>#include<list>#include<future>usingnamespacestd;intmyThread(intnum){cout<<"myThread()startthreadid="<<this_thread::get_i......
  • DisableThreadLibraryCalls与DLLMain死锁
    DisableThreadLibraryCalls与DLLMain死锁 1、首先写个简单的DLL,用来验证1234567891011121314151617181920212223242526272829303132BOOL APIENTRYDllMain( HMODULE hModule,                       ......
  • std::thread 一:创建线程的三种方式
    前言:#include<thread>thread.join()//阻塞thread.detach()//非阻塞thread.joinable()//bool,判断线程是否支持join或者detach 正文:创建线程有三种方式,分别是:使用函数来创建线程、使用自定义的类来创建线程、使用lambda函数来创建线程 一、使用函数来......
  • std::thread 二:互斥量(lock() & unlock())
     mutex 互斥量的作用是保护共享数据*:有lock() 就一定要有 unlock()#include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;i<10000;i++)......
  • std::thread 二:互斥量(lock_guard())
    *:使用lock_guard后,就不可以使用lock()和unlock()*:lock_guard和智能指针一样,会自动解锁 #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;......
  • std::thread 二:互斥量(多个互斥量的解决方法)
     //*:这里的lock是函数模板,最少传两个互斥量//第一种,使用lock和unlockstd::mutexm_mutex1;std::mutexm_mutex2;std::lock(m_mutex1,m_mutex2);m_mutex1.unlock();m_mutex2.unlock();//第二种,使用lock和lock_guardstd::mutexm_mutex1;std::mutexm_m......
  • std::thread 二:互斥量(带超时的互斥量 timed_mutex())
     timed_mutex、 try_lock_for、 try_lock_until #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;i<10000;i++)......
  • std::string 拼接字符串
      #include<iostream>#include<string>#include<sstream>usingnamespacestd;intmain(){stringa="123";stringstreamb;b<<123<<"456"<<789<<"-=-=";......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......