0
点赞
收藏
分享

微信扫一扫

c++线程通讯

东方小不点 2022-02-16 阅读 40

线程通讯通过全局promise变量和线程局部变量future进行通讯。

promise<string> val;//全局通讯变量

int main(){
    
    thread th1([](){
        future<string> fus = val.get_future();//获取未来状态
        cout<<"等待中…"<<endl;
        cout<<fus.get()<<endl;
    });
    thread th2([](){
        system("pause");
        val.set_value("hello cpp");
    });
    th1.join();
    th2.join();
    return 1;
}
举报

相关推荐

0 条评论