《C++ Concurrency in Action》笔记4 hardware_concurrency()
来源:互联网 发布:u盘做linux启动盘 编辑:程序博客网 时间:2024/06/06 01:56
One feature of the C++ Standard Library that helps here is std::thread::hardware_concurrency() . This function returns an indication of the number of threads that can
truly run concurrently for a given execution of a program. On a multicore system it might be the number of CPU cores, for example. This is only a hint, and the functionmight return 0 if this information is not available, but it can be a useful guide for splitting a task among threads.
std::thread::hardware_concurrency()这个函数用于获取程序可以调动的最大线程数,在多核系统中可能代表CPU核数。这个函数返回值仅可以作为参考,因为有可能返回0。
摘抄一段原书中的代码,稍做修改:
template<typename Iterator, typename T>struct accumulate_block{void operator()(Iterator first, Iterator last, T& result){result = std::accumulate(first, last, result);}};template<typename Iterator, typename T>T parallel_accumulate(Iterator first, Iterator last, T init){unsigned long const length = std::distance(first, last);if (!length)return init;unsigned long const min_per_thread = 25;unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread;unsigned long const hardware_threads = std::thread::hardware_concurrency();cout << "hardware_threads:" << hardware_threads << endl;unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);cout << "num_threads:" << num_threads << endl;unsigned long const block_size = length / num_threads;std::vector<T> results(num_threads);std::vector<std::thread> threads(num_threads - 1);Iterator block_start = first;for (unsigned long i = 0; i<(num_threads - 1); ++i){Iterator block_end = block_start;std::advance(block_end, block_size);threads[i] = std::thread(accumulate_block<Iterator, T>(),block_start, block_end, std::ref(results[i]));block_start = block_end;}accumulate_block<Iterator, T>()(block_start, last, results[num_threads - 1]);std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));return std::accumulate(results.begin(), results.end(), init);}void call_by_main(){vector<int> v(51);fill(v.begin(), v.end(), 1);cout << parallel_accumulate(v.begin(), v.end(), 0) << endl;system("pause");}
在我本机上执行的结果是:
hardware_threads:4num_threads:351请按任意键继续. . .
求一段迭代器指定范围的所有元素的和。
阅读全文
0 0
- 《C++ Concurrency in Action》笔记4 hardware_concurrency()
- 《C++ Concurrency in Action》笔记4 vector<thread>
- C plus plus Concurrency in Action
- 读C++ concurrency in action笔记
- 《C++ concurrency in action》第二章笔记
- 《C++ Concurrency in Action》笔记 前言
- 《C++ Concurrency in Action》笔记14 condition_variable
- 《C++ Concurrency in Action》笔记16 future
- 《C++ Concurrency in Action》笔记17 promise
- 《C++ Concurrency in Action》笔记2 线程函数传参(4)move
- 《C++ Concurrency in Action》笔记1 join和detach
- 《C++ Concurrency in Action》笔记3 move线程对象
- 《C++ Concurrency in Action》笔记5 std::thread::id
- 《C++ Concurrency in Action》笔记6 Avoiding problematic race conditions
- 《C++ Concurrency in Action》笔记7 mutex(1)
- 《C++ Concurrency in Action》笔记8 死锁(1)
- 《C++ Concurrency in Action》笔记8 死锁(2)避免死锁
- 《C++ Concurrency in Action》笔记9 std::unique_lock源码分析
- Python IO笔记
- ArrayList源码学习
- 230. Kth Smallest Element in a BST
- 计算机组成原理与系统结构 第一章计算机概述
- Qt5开发学习之事件(十三)
- 《C++ Concurrency in Action》笔记4 hardware_concurrency()
- Linux系统下FTP服务器的搭建
- 九度题目1156:谁是你的潜在朋友
- android实现和web一样的圆角
- 无限轮播+GridView
- Handler机制
- word中的修订的使用
- 数据结构-双向循环链表
- jQuery DOM 元素方法