C++多线程编程第九讲--async、future、packaged_task、promise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//(1)std::async、 std::future创建后台任务并返回值
// async是一个函数模板,用来启动一个异步任务,返回一个future类型的对象。用future
// 的get方法来获得线程的返回值。
// 异步任务:自动创建一个线程,并开始执行对应线程的入口函数。
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
 
int my_thread()
{
    cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
    std::chrono::milliseconds duro(5000);
    std::this_thread::sleep_for(duro);
    cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
    return 5;
}
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    std::future<int> result = std::async(my_thread);
    cout << result.get() << endl;              //主线程会在get这里一致等到新的线程返回结果
    //cout << result.get() << endl;            //不能调用多次,调用多次会报异常
    //result.wait();  //等待新的线程执行结束,但并不会储存返回值。
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
class A
{
public:
    int my_thread(int mypar)
    {
        cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
        std::chrono::milliseconds duro(5000);
        std::this_thread::sleep_for(duro);
        cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
        return mypar;
    }
};
 
 
 
int main()
{
    A myobja;
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    std::future<int> result = std::async(&A::my_thread, std::ref(myobja), 5);
    //主线程会在get这里一致等到新的线程返回结果,如果没有这一句的话
    //在主线程return的时候,会等待新的线程执行结束。
    cout << result.get() << endl;
    //cout << result.get() << endl;            //不能调用多次,调用多次会报异常
    //result.wait();  //等待新的线程执行结束,但并不会储存返回值。
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
// 可以额外向std::async()传递一个参数,参数类型是std::lunch类型(枚举)。
// std::launch::deferred  新线程执行时间延迟到调用wait或者get时才执行(创建)
 
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
class A
{
public:
    int my_thread(int mypar)
    {
        cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
        std::chrono::milliseconds duro(5000);
        std::this_thread::sleep_for(duro);
        cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
        return mypar;
    }
};
 
 
 
int main()
{
    A myobja;
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    //新线程延迟到调用wait或者get的时候再启动,但使用std::launch::deferred这个参数
    //直接没有创建新的线程,在主线程中调用的入口函数。
    std::future<int> result = std::async(std::launch::deferred, &A::my_thread, std::ref(myobja), 5);
    //主线程会在get这里一致等到新的线程返回结果,如果没有这一句的话
    //在主线程return的时候,会等待新的线程执行结束。
    cout << result.get() << endl;                   //在主线程中执行。
    //cout << result.get() << endl;            //不能调用多次,调用多次会报异常
    //result.wait();  //等待新的线程执行结束,但并不会储存返回值。
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
// std::launch::async 在调用async这个函数的时候即开始创建新的线程,执行入口函数。默认async方法的第一个参数
//就是std::launch::async
//
//(2)std::packaged_task   是个模板类,将各种可调用对象包装起来。
 
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
 
int my_thread(int mypar)
{
    cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
    std::chrono::milliseconds duro(5000);
    std::this_thread::sleep_for(duro);
    cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
    return mypar;
}
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
 
    std::packaged_task<int(int)> mypt(my_thread);           //把函数my_thread包装起来
    std::thread t1(std::ref(mypt), 5);                      //用一个package_task对象作为线程的参数
 
    t1.join();
 
    std::future<int> result = mypt.get_future();
 
    cout << result.get() << endl;              //打印返回的结果
 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
  
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
 
    //包装lamda表达式
    std::packaged_task<int(int)> mypt([](int mypar)
        {
            cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
            std::chrono::milliseconds duro(5000);
            std::this_thread::sleep_for(duro);
            cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
            return mypar;
        }
    );
 
    std::thread t1(std::ref(mypt), 5);                      //用一个package_task对象作为线程的参数
 
    t1.join();
 
    std::future<int> result = mypt.get_future();
 
    cout << result.get() << endl;              //打印返回的结果
 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
 
    //包装lamda表达式
    std::packaged_task<int(int)> mypt([](int mypar)
        {
            cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
            std::chrono::milliseconds duro(5000);
            std::this_thread::sleep_for(duro);
            cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
            return mypar;
        }
    );
 
    mypt(5);            //不创建新的线程,直接调用也可以。
 
    std::future<int> result = mypt.get_future();
 
    cout << result.get() << endl;              //打印返回的结果
 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
#include<vector>
 
using namespace std;
 
vector<std::packaged_task<int(int)>> mytasks;
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
 
    //包装lamda表达式
    std::packaged_task<int(int)> mypt([](int mypar)
        {
            cout << "my_thread start...  " << "thread id = " << this_thread::get_id() << endl;
            std::chrono::milliseconds duro(5000);
            std::this_thread::sleep_for(duro);
            cout << "my_thread end...  " << "thread id = " << this_thread::get_id() << endl;
            return mypar;
        }
    );
 
    mytasks.push_back(std::move(mypt));     //使用移动语义
 
    std::packaged_task<int(int)> mypt2;
    auto iter = mytasks.begin();
    mypt2 = std::move(*iter);
 
    mytasks.erase(iter);              //迭代器失效,后面不可以使用iter
 
    mypt2(123);
    std::future<int> result = mypt2.get_future();
    cout << result.get() << endl;
 
 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
//(3)std::promise
// 我们可以在某个线程中给他赋值,在其他线程中把这个值取出来。
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
#include<vector>
#include<memory>
 
using namespace std;
 
void my_thread(std::promise<int>& tmp, int calc)
{
    cout << "my_thread start..." << endl;
    cout << "my_thread id = " << std::this_thread::get_id() << endl;
    cout << calc << endl;
    std::chrono::milliseconds duro(5000);   //休眠5秒,模拟计算过程
    std::this_thread::sleep_for(duro);
 
 
    int result = calc;
 
    tmp.set_value(result);    //将结果保存到promise的对象中
 
    cout << "my_thread end..." << endl;
}
 
void my_thread2(std::future<int>& tmpf)
{
    cout << "my_thread2 start..." << endl;
    cout << "thread id = " << std::this_thread::get_id() << endl;
    cout << tmpf.get() << endl;   //获得另外一个线程传过来的值
 
    cout << "my_thread2 end..." << endl;
}
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
     
    std::promise<int> result;
    //std::thread t1(my_thread, result, 123);                 //错误
    std::thread t1(my_thread, std::ref(result), 123);         //使用ref来传递,不能直接赋值对象。
    auto fut = result.get_future();
    std::thread t2(my_thread2, std::ref(fut));
 
    t1.join();                               
    t2.join();
 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}

  

posted on   xcxfury001  阅读(69)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示