C++ 同時マルチスレッド -- std::async、std::packages_task、std::promise の使用

目次

1 -- std::async の使用

2 -- std::packages_task の使用

3 -- std::promise の使用


1 -- std::async の使用

        std::async は、非同期タスクを開始し、 std::future オブジェクトを返すために使用されます。std::future オブジェクトには、非同期タスク スレッド エントリ関数の結果が含まれます。

        std::launch::deferred は、スレッド エントリ関数の呼び出しが、std::future の wait() または get() 呼び出しまで遅延されることを意味します。wait() または get() が呼び出されない場合、スレッド エントリ関数はCall は呼び出されません (スレッドは作成されません)。

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

class Sample{
public:
    // 线程入口函数
    int thread(int value){
        std::cout << "thread id: " << std::this_thread::get_id() << std::endl;
        std::chrono::microseconds dura(value); // rest
        std::this_thread::sleep_for(dura);
        return 5;
    }

};

int main(int argc, char *argv[]){
    Sample sample;
    int value = 5000;
    // std::async用于启动一个异步任务,并返回一个std::future对象
    // std::future对象里含有异步任务线程入口函数的结果
    std::cout << "thread id: " << std::this_thread::get_id() << std::endl;
    std::future<int> result = std::async(&Sample::thread, &sample, value);

    // std::launch::deferred 表示调用线程入口函数将会被延迟到 std::future 的wait()或get()调用
    // 当wait()或者get()没有被调用时,线程入口函数不会被调用(线程不会被创建)
    // std::future<int> result = std::async(std::launch::deferred, &Sample::thread, &sample, value);

    // result.get()等待thread()执行完毕获取结果后,主线程才继续往下执行
    std::cout << "resule.get(): " << result.get() << std::endl; 

    // result.wait() // 等待线程返回,但不返回结果

    std::cout << "main thread continue ..." << std::endl;
    return 0;
}

2 -- std::packages_task の使用

        std::packages_task は、さまざまな呼び出し可能なオブジェクトをパッケージ化するタスクをパッケージ化するために使用され、後でスレッド エントリ関数として使用できるようになります。

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

// 线程入口函数
int thread(int value){
    std::cout << "thread id: " << std::this_thread::get_id() << std::endl;
    std::chrono::microseconds dura(value); // rest
    std::this_thread::sleep_for(dura);
    return 5;
}

int main(int argc, char *argv[]){
    // std::packaged_task 用于打包任务,其包装各种可调用对象,方便后续作为线程入口函数
    std::cout << "thead id: " << std::this_thread::get_id() << std::endl;
    std::packaged_task<int(int)> mypt(thread);
    int value = 5000;
    std::thread thread1(std::ref(mypt), value);
    thread1.join();
    std::future<int> result = mypt.get_future();
    std::cout << "result.get(): " << result.get() << std::endl;
    return 0;
}

3 -- std::promise の使用

        std::promise は、スレッドの値を他のスレッドで使用するために使用されます。次のコード例では、thread2 は thread1 の結果値を使用します。

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

// 线程入口函数
int thread(std::promise<int> &tmpp, int clac){
    clac++;
    clac *= 10; 
    std::cout << "thread id: " << std::this_thread::get_id() << std::endl;
    int result = clac;
    tmpp.set_value(result);
    return 0;
}

void thread_2(std::future<int> &tmpf){
    auto result2 = tmpf.get();
    std::cout << "tmpf.get(): " << result2 << std::endl;
}

int main(int argc, char *argv[]){
    // std::promise 用于在某个线程中赋值,并在其他线程中将值取来用
    std::cout << "thead id: " << std::this_thread::get_id() << std::endl;
    std::promise<int> prom; 
    int clac = 1;
    std::thread thread1(thread, std::ref(prom), clac);
    thread1.join();

    // 将promise中的值取来用
    std::future<int> result = prom.get_future();
    std::thread thread2(thread_2, std::ref(result));
    thread2.join();
    return 0;
}

おすすめ

転載: blog.csdn.net/weixin_43863869/article/details/132369332