[Modern C++]现代C++中的异步操作

开发 后端
如果在输出中间信息的时候不先现在字符串里输出会很乱,因为同一行cout输出不同项的操作不是原子的,中间可能会转到其他线程去。

[[174007]]

背景

老实说,C++11中对异步的支持不必Python和JavaScript弱。

实现(代码)

  1. #include <iostream> 
  2. #include <vector> 
  3. #include <algorithm> 
  4. #include <future> 
  5.  
  6. template <typename RAIter> 
  7. int parallel_sum(RAIter front, RAIter beg, RAIter end
  8.     auto len = end - beg; 
  9.     if (len < 1000) 
  10.     { 
  11.         char out[50]; 
  12.         sprintf(out"Calculate from %d to %d\n", beg - front, end - front); 
  13.         std::cout << out
  14.         return std::accumulate(beg, end, 0); 
  15.     } 
  16.  
  17.     RAIter mid = beg + len / 2; 
  18.     auto handle_right = std::async(std::launch::async, 
  19.                              parallel_sum<RAIter>, front, mid, end); 
  20.     auto handle_left = std::async(std::launch::async, 
  21.                              parallel_sum<RAIter>, front, beg, mid); 
  22.     return handle_left.get() + handle_right.get(); 
  23.  
  24. int main() 
  25.     std::vector<int> v(20000, 1); 
  26.     std::cout << "The sum is " << parallel_sum(v.begin(), v.begin(), v.end()) << '\n'

输出

可能一

  1. Calculate from 8125 to 8750                                                                                                                
  2. Calculate from 18125 to 18750                                                                                                              
  3. Calculate from 10625 to 11250                                                                                                              
  4. Calculate from 2500 to 3125                                                                                                                
  5. Calculate from 13750 to 14375                                                                                                              
  6. Calculate from 5000 to 5625                                                                                                                
  7. Calculate from 1250 to 1875                                                                                                                
  8. Calculate from 3750 to 4375                                                                                                                
  9. Calculate from 16250 to 16875                                                                                                              
  10. Calculate from 17500 to 18125                                                                                                              
  11. Calculate from 10000 to 10625                                                                                                              
  12. Calculate from 625 to 1250                                                                                                                 
  13. Calculate from 18750 to 19375                                                                                                              
  14. Calculate from 15625 to 16250                                                                                                              
  15. Calculate from 3125 to 3750                                                                                                                
  16. Calculate from 12500 to 13125                                                                                                              
  17. The sum is 20000  

可能二

  1. Calculate from 1875 to 2500 
  2. Calculate from 3750 to 4375  
  3. Calculate from 8750 to 9375  
  4. Calculate from 7500 to 8125  
  5. Calculate from 15000 to 15625  
  6. Calculate from 10000 to 10625  
  7. Calculate from 1250 to 1875  
  8. Calculate from 6875 to 7500  
  9. Calculate from 0 to 625 
  10. Calculate from 3125 to 3750  
  11. Calculate from 5625 to 6250  
  12. Calculate from 5000 to 5625  
  13. Calculate from 6250 to 6875  
  14. Calculate from 2500 to 3125  
  15. The sum is 20000 

可能.........

注意

如果在输出中间信息的时候不先现在字符串里输出会很乱,因为同一行cout输出不同项的操作不是原子的,中间可能会转到其他线程去。

责任编辑:武晓燕 来源: segmentfault
相关推荐

2010-01-21 09:34:57

C++语法

2010-01-27 16:05:06

C++堆栈

2022-09-21 13:53:15

C++移动语义

2010-01-21 14:07:14

CC++声明

2010-01-25 10:25:19

C++变量

2010-01-27 17:16:52

C++构造函数

2010-01-20 10:19:55

C++数组

2010-01-28 16:31:54

C++类型

2010-01-21 13:33:44

C++基类

2020-08-21 13:20:36

C++If ElseLinux

2023-11-17 11:40:51

C++内存

2010-01-26 10:42:26

C++函数

2023-10-04 00:38:30

C++原子

2010-01-26 14:35:11

C++关键字

2023-11-13 22:30:16

C++开发

2010-01-28 13:27:12

C++类定义

2010-01-28 11:08:09

C++变量

2010-01-19 13:43:59

C++函数

2011-06-17 16:09:04

freadfwrite

2010-01-20 09:54:27

C++数据类型
点赞
收藏

51CTO技术栈公众号