C++11中线程间的异步通信

一、std::promise 和 std::future 的基本概念

在C++11中,std::promise 和 std::future 是用于实现异步编程的重要工具。它们提供了一种机制,允许一个线程将值或异常传递给另一个线程,从而实现线程间的数据传递和同步。

std::promise:

  • 是一个模板类,用于存储一个值或异常
  • 可以通过 set_value() 设置值,或者通过 set_exception() 设置异常
  • 与 std::future 关联,用于将结果传递给其他线程。

std::future:

  • 是一个模板类,用于从 std::promise 中获取值或异常。
  • 可以通过 get() 方法获取结果。如果结果尚未准备好,get() 会阻塞当前线程,直到结果可用。
  • 只能获取一次结果,调用 get() 后,future 的状态会变为无效。

二、std::promise 和 std::future 的基本使用

以下是一个简单的示例,展示如何使用 std::promise 和 std::future 在线程间传递数据:

#include <iostream>
#include <thread>
#include <future>
#include <chrono>

void task(std::promise<int> promise) {
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
    promise.set_value(42); // 设置结果
}

int main() {
    std::promise<int> promise;
    std::future<int> future = promise.get_future(); // 获取与 promise 关联的 future

    std::thread t(task, std::move(promise)); // 启动线程,传递 promise

    std::cout << "Waiting for result..." << std::endl;
    int result = future.get(); // 阻塞等待结果
    std::cout << "Result: " << result << std::endl;

    t.join(); // 等待线程结束
    return 0;
}

使用 std::promise 传递异常:

#include <iostream>
#include <thread>
#include <future>
#include <exception>

void task(std::promise<int> promise) {
    try {
        throw std::runtime_error("An error occurred!"); // 抛出异常
    } catch (...) {
        promise.set_exception(std::current_exception()); // 捕获并传递异常
    }
}

int main() {
    std::promise<int> promise;
    std::future<int> future = promise.get_future();

    std::thread t(task, std::move(promise));

    try {
        int result = future.get(); // 尝试获取结果
        std::cout << "Result: " << result << std::endl;
    } catch (const std::exception &e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    t.join();
    return 0;
}

std::future 的其他用法
wait():

  • 阻塞当前线程,直到结果可用。
  • 不返回结果,仅用于同步。

wait_for() 和 wait_until():

  • 允许设置超时时间,避免无限期阻塞。
#include <iostream>
#include <future>
#include <chrono>

int main() {
    std::promise<int> promise;
    std::future<int> future = promise.get_future();

    std::thread t([&promise]() {
        std::this_thread::sleep_for(std::chrono::seconds(2));
        promise.set_value(100);
    });

    std::future_status status;
    do {
        status = future.wait_for(std::chrono::milliseconds(500)); // 等待 500ms
        if (status == std::future_status::timeout) {
            std::cout << "Still waiting..." << std::endl;
        }
    } while (status != std::future_status::ready);

    std::cout << "Result: " << future.get() << std::endl;

    t.join();
    return 0;
}

输出:
在这里插入图片描述
std::promise 和 std::future的使用细节
适用场景:

  • 线程间通信:一个线程计算结果,另一个线程获取结果。
  • 异步任务:将任务交给一个线程执行,主线程通过 future 获取结果。
  • 异常传递:将子线程中的异常传递到主线程。

注意事项:

  • std::future 只能获取一次结果:调用 get() 后,future 的状态会变为无效,再次调用会导致未定义行为。
  • std::promise 和 std::future 不可复制:只能通过 std::move 传递。
  • 线程安全::std::promise 和 std::future 是线程安全的,可以在多线程环境中使用。

三、std::async的基本用法

在C++11中,std::async 是一个用于异步执行任务的函数模板。它提供了一种简单的方式来启动异步任务,并返回一个 std::future 对象,用于获取任务的执行结果。std::async 是 C++11 并发编程的重要工具之一,可以替代手动创建和管理线程的复杂性。

1. std::async 的基本概念

  • 异步执行:std::async 允许在后台异步执行一个函数或可调用对象,而不会阻塞当前线程。
  • 返回值:返回一个 std::future 对象,用于获取异步任务的结果。
  • 启动策略:可以通过启动策略控制任务的执行方式(立即启动、延迟启动等)。

2. std::async 的语法

#include <future>

template <class Function, class... Args>
std::future<std::result_of_t<Function(Args...)>> async(Function&& f, Args&&... args);

template <class Function, class... Args>
std::future<std::result_of_t<Function(Args...)>> async(std::launch policy, Function&& f, Args&&... args);

参数:

  • f: 要执行的函数或可调用对象。
  • args: 传递给函数的参数。
  • policy: 启动策略(可选)。

返回值:

  • 返回一个 std::future 对象,用于获取任务的结果。

3. 启动策略
std::async 支持两种启动策略,通过 std::launch 枚举指定:

  • std::launch::async:任务会在一个新线程中立即异步执行。
  • std::launch::deferred:任务会延迟执行,直到调用 std::future::get() 或 std::future::wait() 时才在当前线程中执行。
  • 默认策略:如果不指定策略,std::async 的实现可以选择 std::launch::async 或 std::launch::deferred,或者两者的组合。

4. std::async 的基本使用
以下是一个简单的示例,展示如何使用 std::async 异步执行任务并获取结果:

#include <iostream>
#include <future>
#include <chrono>

int task() {
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
    return 42;
}

int main() {
    // 启动异步任务
    std::future<int> future = std::async(std::launch::async, task);

    std::cout << "Waiting for result..." << std::endl;
    int result = future.get(); // 阻塞等待结果
    std::cout << "Result: " << result << std::endl;

    return 0;
}

5. 使用 std::async 传递参数
std::async 可以传递参数给任务函数。以下是一个示例:

#include <iostream>
#include <future>

int add(int a, int b) {
    return a + b;
}

int main() {
    // 启动异步任务并传递参数
    std::future<int> future = std::async(std::launch::async, add, 10, 20);

    int result = future.get(); // 获取结果
    std::cout << "Result: " << result << std::endl;

    return 0;
}

6. 使用 std::async 处理异常
如果任务函数抛出异常,异常会被捕获并存储在 std::future 中。调用 future.get() 时,异常会重新抛出。

#include <iostream>
#include <future>
#include <exception>

int task() {
    throw std::runtime_error("An error occurred!");
    return 42;
}

int main() {
    std::future<int> future = std::async(std::launch::async, task);

    try {
        int result = future.get(); // 尝试获取结果
        std::cout << "Result: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    return 0;
}

7. 使用 std::async 的启动策略

#include <iostream>
#include <future>
#include <chrono>

int task() {
    std::cout << "Task running on thread: " << std::this_thread::get_id() << std::endl;
    return 42;
}

int main() {
    // 延迟启动任务
    std::future<int> future = std::async(std::launch::deferred, task);

    std::cout << "Main thread: " << std::this_thread::get_id() << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));

    int result = future.get(); // 任务在当前线程中执行
    std::cout << "Result: " << result << std::endl;

    return 0;
}

8. std::async 的适用场景

  • 异步计算:将耗时的计算任务放到后台执行,避免阻塞主线程。
  • 并行任务:同时启动多个任务,充分利用多核 CPU。
  • 简化线程管理:替代手动创建和管理线程的复杂性。

9. 注意事项

  • std::future 的生命周期:确保 std::future 对象的生命周期足够长,以避免任务未完成时 future 被销毁。
  • 启动策略的选择:如果不指定策略,std::async 的行为可能因实现而异。如果需要明确的行为,应显式指定策略。
  • 性能开销:std::async 可能会创建新线程,带来一定的性能开销。对于非常小的任务,可能不适合使用。

四、std::packaged_task的基本用法

在C++11中,std::packaged_task 是一个模板类,用于将可调用对象(如函数、Lambda表达式等)包装成一个异步任务,并与 std::future 关联,以便在任务完成后获取结果。std::packaged_task 通常用于将任务与线程分离,使得任务可以在任意线程中执行,同时通过 std::future 获取任务的结果。

1. std::packaged_task 的基本概念

  • 任务包装器:std::packaged_task 是一个可调用对象的包装器,可以将函数、Lambda表达式等包装成一个任务。
  • 每个 std::packaged_task 都与一个 std::future 关联,用于获取任务的执行结果。
  • 线程分离:任务可以在任意线程中执行,而不需要直接与线程绑定。

2. std::packaged_task 的基本使用
以下是一个简单的示例,展示如何使用 std::packaged_task 包装任务并通过 std::future 获取结果:

#include <iostream>
#include <future>
#include <thread>
#include <chrono>

int task() {
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
    return 42;
}

int main() {
    // 创建一个 packaged_task,包装任务函数
    std::packaged_task<int()> packaged_task(task);

    // 获取与任务关联的 future
    std::future<int> future = packaged_task.get_future();

    // 在另一个线程中执行任务
    std::thread t(std::move(packaged_task)); // packaged_task 不可复制,必须使用 std::move

    std::cout << "Waiting for result..." << std::endl;
    int result = future.get(); // 阻塞等待结果
    std::cout << "Result: " << result << std::endl;

    t.join(); // 等待线程结束
    return 0;
}

3. 使用 std::packaged_task 传递参数
std::packaged_task 可以包装带参数的任务。以下是一个示例:

#include <iostream>
#include <future>
#include <thread>

int add(int a, int b) {
    return a + b;
}

int main() {
    // 创建一个 packaged_task,包装带参数的任务函数
    std::packaged_task<int(int, int)> packaged_task(add);

    // 获取与任务关联的 future
    std::future<int> future = packaged_task.get_future();

    // 在另一个线程中执行任务
    std::thread t(std::move(packaged_task), 10, 20); // 传递参数

    int result = future.get(); // 获取结果
    std::cout << "Result: " << result << std::endl;

    t.join(); // 等待线程结束
    return 0;
}

4. 使用 std::packaged_task 处理异常
如果任务函数抛出异常,异常会被捕获并存储在 std::future 中。调用 future.get() 时,异常会重新抛出。

#include <iostream>
#include <future>
#include <thread>
#include <exception>

int task() {
    throw std::runtime_error("An error occurred!");
    return 42;
}

int main() {
    std::packaged_task<int()> packaged_task(task);
    std::future<int> future = packaged_task.get_future();

    std::thread t(std::move(packaged_task));

    try {
        int result = future.get(); // 尝试获取结果
        std::cout << "Result: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    t.join();
    return 0;
}

5. 使用 std::packaged_task 重置任务
std::packaged_task 的 reset() 函数可以重置任务状态,使其可以重新执行。

#include <iostream>
#include <future>
#include <thread>

int task() {
    static int count = 0;
    return ++count;
}

int main() {
    std::packaged_task<int()> packaged_task(task);

    // 第一次执行任务
    std::future<int> future1 = packaged_task.get_future();
    std::thread t1(std::move(packaged_task));
    t1.join();
    std::cout << "Result 1: " << future1.get() << std::endl;

    // 重置任务
    packaged_task.reset();
    std::future<int> future2 = packaged_task.get_future();
    std::thread t2(std::move(packaged_task));
    t2.join();
    std::cout << "Result 2: " << future2.get() << std::endl;

    return 0;
}

6. std::packaged_task 的适用场景

  • 任务队列:将任务包装成 std::packaged_task 并放入队列中,由线程池或其他线程执行。
  • 异步任务管理:将任务与线程分离,通过 std::future 获取结果。
  • 并行计算:将计算任务分发到多个线程中执行,并通过 std::future 收集结果。

7. 注意事项

  • std::packaged_task 只能移动(move),不能复制。
  • 确保 std::future 对象的生命周期足够长,以避免任务未完成时 future 被销毁。
  • std::packaged_task 本身不是线程安全的,需要在多线程环境中小心使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值