C++多线程编程第十讲--future其他成员函数、shared_future、atomic

//(1)std::future的其他成员函数

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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#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);
    std::future_status status = result.wait_for(std::chrono::milliseconds(6000));  //等待
    if (status == std::future_status::timeout)
    {
        //超时表示等待的线程还没有执行结束
        cout << "overtime..." << endl;
    }
    else if (status == std::future_status::ready)
    {
        cout << "ready..." << endl;
    }
    else if (status == std::future_status::deferred)  //线程还没开始执行
    {
        cout << "deferred..." << endl;           //对应async中第一个参数中的std::launch::deferred
    }
 
    cout << result.get() << endl;              //主线程会在get这里一致等到新的线程返回结果
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
//(2)std::shared_future
// 是一个类模板,他的get函数就不是转移了,而是复制。
//
// 如以下程序,只能在线程2中调用get,如果还有其他线程想要线程1返回的结果再次调用get的时候
// 程序就会报错
#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;
}
 
void my_thread2(std::future<int>& fut)
{
    cout << "my_thread2 start..." << "thread id = " << this_thread::get_id() << endl;
    //程序运行到这里,即便线程1还没有执行结束,调用get也会等待线程1返回结果再运行
    cout << "fut.get() = " << fut.get() << endl;  //打印返回的结果,get函数设计是一个移动语义,所以不能调用两次。
    cout << "my_thread2 end..." << "thread id = " << this_thread::get_id() << endl;
}
 
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对象作为线程的参数
 
    std::future<int> result = mypt.get_future();
 
    //std::shared_future<int> s_fut(std::move(result));    //使用右值
 
 
    std::thread t2(my_thread2, std::ref(result));     //future对象不能拷贝,所以只能以std::ref的方式进行传递
    t2.join();
    t1.join();
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
// 则使用shared_future避免以上的情况
 
#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;
}
 
void my_thread2(std::shared_future<int>& s_fut)
{
    cout << "my_thread2 start..." << "thread id = " << this_thread::get_id() << endl;
    //程序运行到这里,即便线程1还没有执行结束,调用get也会等待线程1返回结果再运行
    cout << "fut.get() = " << s_fut.get() << endl;  //get只是复制,可以调用多次
    cout << "my_thread2 end..." << "thread id = " << this_thread::get_id() << endl;
}
 
void my_thread3(std::shared_future<int>& s_fut)
{
    cout << "my_thread3 start..." << "thread id = " << this_thread::get_id() << endl;
    //程序运行到这里,即便线程1还没有执行结束,调用get也会等待线程1返回结果再运行
    cout << "fut.get() = " << s_fut.get() << endl;  //get只是复制,可以调用多次
    cout << "my_thread3 end..." << "thread id = " << this_thread::get_id() << endl;
}
 
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对象作为线程的参数
 
    std::future<int> result = mypt.get_future();
 
    //bool value = result.valid();  //可以用来判断result中是否有有效值
 
    std::shared_future<int> s_fut(std::move(result));    //使用右值
    //std::shared_future<int> s_fut(result.share());    //使用右值,两种写法都可以
 
    std::thread t2(my_thread2, std::ref(s_fut));     //future对象不能拷贝,所以只能以std::ref的方式进行传递
    std::thread t3(my_thread3, std::ref(s_fut));     //future对象不能拷贝,所以只能以std::ref的方式进行传递
    t2.join();
    t1.join();
    t3.join();
    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 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;
}
 
void my_thread2(std::shared_future<int>& s_fut)
{
    cout << "my_thread2 start..." << "thread id = " << this_thread::get_id() << endl;
    //程序运行到这里,即便线程1还没有执行结束,调用get也会等待线程1返回结果再运行
    cout << "fut.get() = " << s_fut.get() << endl;  //get只是复制,可以调用多次
    cout << "my_thread2 end..." << "thread id = " << this_thread::get_id() << endl;
}
 
void my_thread3(std::shared_future<int>& s_fut)
{
    cout << "my_thread3 start..." << "thread id = " << this_thread::get_id() << endl;
    //程序运行到这里,即便线程1还没有执行结束,调用get也会等待线程1返回结果再运行
    cout << "fut.get() = " << s_fut.get() << endl;  //get只是复制,可以调用多次
    cout << "my_thread3 end..." << "thread id = " << this_thread::get_id() << endl;
}
 
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对象作为线程的参数
 
    //bool value = result.valid();  //可以用来判断result中是否有有效值
 
    std::shared_future<int> s_fut(mypt.get_future());    //使用右值
    //std::shared_future<int> s_fut(result.share());    //使用右值,两种写法都可以
 
    std::thread t2(my_thread2, std::ref(s_fut));     //future对象不能拷贝,所以只能以std::ref的方式进行传递
    std::thread t3(my_thread3, std::ref(s_fut));     //future对象不能拷贝,所以只能以std::ref的方式进行传递
    t2.join();
    t1.join();
    t3.join();
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
//(3)原子操作std::atomic
    //(3.1)原子操作概念引出范例
 
 #include<iostream>
#include<mutex>
#include<thread>
#include<future>
 
using namespace std;
 
int g_mycount = 0;
mutex g_my_mutex;
 
void my_thread()
{
    int i = 0;
    for (; i < 1000000; ++i)
    {
        g_my_mutex.lock();          //防止++的执行被打断
        g_mycount++;
        g_my_mutex.unlock();
    }
}
 
 
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
 
    thread mythread(my_thread);
 
    thread mythread2(my_thread);
 
    mythread.join();
    mythread2.join();  
 
    cout << g_mycount << endl; 
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
 
// 但是以上的程序效率太低,原子操作也会有相同的作用。互斥量的加锁针对的是一个代码段,而原子操作
// 针对的是一个变量。原子操作用于统计功能等。
 
//(3.2)基本的std::atomic用法范例
  #include<iostream>
    #include<mutex>
    #include<thread>
    #include<future>
     
    using namespace std;
     
     
    //int g_mycount = 0;
    std::atomic<int> g_mycount = 0;   //具备原子操作的整型值
     
    void my_thread()
    {
        int i = 0;
        for (; i < 1000000; ++i)
        {
            g_mycount++;
        }
    }
     
     
    int main()
    {
        cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
     
        thread mythread(my_thread);
     
        thread mythread2(my_thread);
     
        mythread.join();
        mythread2.join();
     
        cout << g_mycount << 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 g_mycount = 0;
    std::atomic<bool> g_ifend = false;
     
    void my_thread()
    {
        std::chrono::milliseconds dura(1000); //
     
        while (g_ifend == false)
        {
            cout << "my_thread id = " << std::this_thread::get_id() << endl;
            std::this_thread::sleep_for(dura);
        }
    }
     
    int main()
    {
        cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
     
        thread mythread(my_thread);
     
        thread mythread2(my_thread);
     
        std::chrono::milliseconds dura(5000); //
        std::this_thread::sleep_for(dura);
     
        g_ifend = true;
     
        mythread.join();
        mythread2.join();
     
        cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
        return 0;
    }

  

posted on   xcxfury001  阅读(46)  评论(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

统计

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