栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++11 多线程 future/promise 使用简介

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++11 多线程 future/promise 使用简介

启动一个线程,并取出其中的一个变量,不使用future/promise时,代码大致如下:

#include 

using namespace std;

// 对同一变量进行操作,加锁
std::mutex mu;
// 子线程先设置 x,加信号量
std::condition_variable cond;

void factorial(int N) {
    int res = 1;
    for (int i = N; i > 1; i -- )
        res *= i;
    
    cout << "Result is: " << res << endl;
}

// unlock
// uncondition

int main() {
    int x;
    std::thread t1(factorial, 4, std::ref(x));

    t1.join();
    return 0;
}

代码逻辑简单时还好说,要是逻辑复杂再加上几层回调,代码可读性很低,这时候就轮到future/promise上场了

启动一个线程,并取出其中的一个变量,使用future/promise实现的大致代码如下:

#include 
#include 

using namespace std;

int factorial(int& N) {
    int res = 1;
    for (int i = N; i > 1; i -- )
        res *= i;
    
    cout << "Result is: " << res << endl;
    return res;
}

int main() {
    int x;
    // future object 表示我们可以在未来获取某个东西
    // std::async function may  or may not create another thread, 
    // which control by another parameter: std::launch::defeered (same thread),             
    std::launch::async (new thread)
    std::future fu = std::async(factorial, std::ref(x));
    x = fu.get(); // get只能使用一次

    return 0;
}

从父线程向子线程传递参数,并且该参数在未来产生

#include 
#include 

using namespace std;

int factorial(std::function& f) {
    int res = 1;
    int N = f.get();
    for (int i = N; i > 1; i -- )
        res *= i;
    
    cout << "Result is: " << res << endl;
    return res;
}

int main() {
    int x;

    std::promise p;
    std::future f = p.get_future();
    // tell child thread that a value will be sent in the future
    std::future fu = std::async(std::launch::async, factorial, std::ref(f));

    // do something else
    std::this_thread::sleep_for(chrono::milliseconds(20));
    // p.set_exception(std::make_except_ptr(std::runtime_error("To err is human")));
    p.set_value(4);

    x = fu.get(); // get 只能使用一次

    return 0;
}

开十个线程的情况,可使用shared_future

#include 
#include 

using namespace std;

int factorial(std::shared_function f) {
    int res = 1;
    int N = f.get();
    for (int i = N; i > 1; i -- )
        res *= i;
    
    cout << "Result is: " << res << endl;
    return res;
}

int main() {
    int x;

    std::promise p;
    std::future f = p.get_future();
    std::shared_future sf = f.share();

    // tell child thread that a value will be sent in the future
    std::future fu = std::async(std::launch::async, factorial, sf);
    std::future fu1 = std::async(std::launch::async, factorial, sf);
    std::future fu2 = std::async(std::launch::async, factorial, sf);
    // 10 threads...

    // do something else
    std::this_thread::sleep_for(chrono::milliseconds(20));
    // p.set_exception(std::make_except_ptr(std::runtime_error("To err is human")));
    p.set_value(4);

    x = fu.get(); // get 只能使用一次

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号