c++五分钟搞定异步处理

future的使用

c++11异步操作提供了future相关的类,std::future、std::promise和std::packaged_task,std::future比std::thread高级些,std::future作为异步结果的传输通道,通过get_future()和get()可以很方便的获取线程函数的返回值,std::promise用来包装一个值,将数据和future绑定起来,而std::packaged_task则用来包装一个可调用函数,将函数和future绑定起来,该函数返回值可以在特定时间获得。future是不可以复制的,源码中future( const future& other ) = delete;可以看到拷贝构造器已被删除。

promise与future配合使用

用两个例子供大家理解,第一个在子线程获取主线程的数据,第二个例子为主线程获取子线程数据。

c 复制代码
#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

void func(std::future<int>& fut) {
    int x = fut.get();
    cout << "value: " << x << endl;
}

int main() {
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();
    std::thread t(func, std::ref(fut));
    prom.set_value(100);
    t.join();
    return 0;
}

c 复制代码
#include <iostream>
#include <thread>
#include <future>
#include <functional>
#include <string>
using namespace std;
int main()
{
    promise<string> pr;
    thread t2([](promise<string> &p)
              { p.set_value("hello"); 
              this_thread::sleep_for(chrono::seconds(2));
       }, ref(pr));
    future<string> f2 = pr.get_future();
    cout << "f2.get(): " << f2.get() << endl;
    t2.join();
    return 0;
}

需要强调,promise对象必须要通过引用的方式传递,在实例化子线程对象的时候,任务函数的参数是引用类型,那么实参需要放到std::ref()函数中,表示实参的引用传递

packaged_task与future配合使用

arduino 复制代码
#include <iostream>
#include <thread>
#include <future>
#include <functional>
using namespace std;

int main()
{
    function<int(int)> f1 = [](int x)
    {
        return x * 100;
    };
    packaged_task<int(int)> task(f1);

    thread t1(ref(task), 100);

    future<int> f = task.get_future();
    int value = f.get();
    std::cout << "value: " << value << endl;
    t1.join();
    return 0;
}

c 复制代码
#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

int func(int in) {
    return in + 1;
}

int main() {
    std::packaged_task<int(int)> task(func);
    std::future<int> fut = task.get_future();
    std::thread(std::move(task), 5).detach();
    cout << "result " << fut.get() << endl;
    return 0;
}

三者之间的关系

future用于访问异步操作的结果,而promise和packaged_task在future高一层,它们内部都有一个future,promise包装的是一个值,packaged_task包装的是一个函数,当需要获取线程中的某个值,可以使用promise,当需要获取线程函数返回值,可以使用packaged_task。

async的使用

async是比future,packaged_task,promise更高级的东西,它是基于任务的异步操作,通过async可以直接创建异步的任务,返回的结果会保存在future中,不需要像packaged_task和promise那么麻烦,关于线程操作应该优先使用async

c 复制代码
#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

int func(int in) { return in + 1; }

int main() {
    auto res = std::async(func, 5);
    // res.wait();
    cout << res.get() << endl;
    return 0;
}

async具体语法如下:async(std::launch::async | std::launch::deferred, func, args...);

  • std::launch::async表示任务执行在另一线程
  • std::launch::deferred表示延迟执行任务,调用get或者wait时才会执行,不会创建线程,惰性执行在当前线程。代码说明
c 复制代码
#include <iostream>
#include <future>
#include <chrono>
int computeSomething(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x * x; 
}

int main() {
    std::future<int> resultFuture = std::async(std::launch::async, computeSomething, 5);
    int result = resultFuture.get();
    std::cout << "Result: " << result << std::endl;
    return 0;
}

c 复制代码
#include <iostream>
#include <future>
#include <chrono>
int computeSomething(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x * x;
}

int main() {
    // 使用 std::async 启动异步任务,使用 std::launch::deferred 策略
    std::future<int> resultFuture = std::async(std::launch::deferred, computeSomething, 5);
    int result = resultFuture.get();
    std::cout << "Result: " << result << std::endl;
    return 0;
}

总结

  • std::async:最简单的方式启动异步任务,适合大多数需要异步执行函数并获取结果的场景。最推荐使用
  • std::promise:提供了一种手动控制异步任务结果的方式,适合需要显式设置值或异常的场景。
  • std::packaged_task:提供了将可调用对象包装成任务的能力,适合需要灵活管理任务的场景。
相关推荐
hqwest2 分钟前
QT肝8天13--删除用户
开发语言·c++·qt·csdn开发云·列表分页·qt分页
奔跑吧邓邓子2 小时前
【C++实战(64)】C++ 邂逅SQLite3:数据库编程实战之旅
数据库·c++·sqlite·实战·sqlite3·数据库编程
会开花的二叉树2 小时前
RabbitMQ C++ 客户端封装与实战
c++·rabbitmq·ruby
Vect__5 小时前
从直线到环形:解锁栈、队列背后的空间与效率平衡术
数据结构·c++
头发还没掉光光7 小时前
C++STL之list
c语言·数据结构·c++·list
我笑了OvO8 小时前
C++类和对象(1)
java·开发语言·c++·类和对象
_屈臣_10 小时前
卡特兰数【模板】(四个公式模板)
c++·算法
渡我白衣10 小时前
C++ 异常处理全解析:从语法到设计哲学
开发语言·c++·面试
青草地溪水旁11 小时前
设计模式(C++)详解——观察者模式(Observer)(1)
c++·观察者模式·设计模式
奔跑吧邓邓子12 小时前
【C++实战(62)】从0到1:C++打造TCP网络通信实战指南
c++·tcp/ip·实战·tcp·网络通信