zoukankan      html  css  js  c++  java
  • 1.future线程通信

     1 #include <future>
     2 #include<iostream>
     3 #include <thread>
     4 #include <thread>
     5 #include <string>
     6 using namespace std;
     7 
     8 //全局通信变量
     9 promise<string> val;
    10 
    11 void main()
    12 {
    13     auto fun1 = []() 
    14     {
    15         //一直等待,获取全局变量的值
    16         future<string> fu = val.get_future();
    17         cout << "Waitting" << endl;
    18         cout <<"获取线程消息" << fu.get() << endl;
    19     };
    20 
    21     auto fun2 = []()
    22     {
    23         system("pause");
    24         //val.set_value("hello");
    25         string str1;
    26         cin >> str1;
    27         val.set_value(str1);
    28         system("pause");
    29     };
    30 
    31     thread t1(fun1);
    32     thread t2(fun2);
    33     
    34     t1.join();
    35     t2.join();
    36     system("pause");
    37 }
  • 相关阅读:
    项目总结升级2
    项目总结升级1
    项目总结升级
    项目总结4
    项目总结3
    体温填报app2.0开发
    每日博客
    第一周开课博客
    学习日报
    学习日报
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8688976.html
Copyright © 2011-2022 走看看