当前位置: 首页 > news >正文

广告联盟赚钱平台优化课程

广告联盟赚钱平台,优化课程,微梦网站建设,建设银行六安市分行网站在运行时选择线程数量 C标准库中对此有所帮助的特性是std::thread::hardware_currency()。这个函数返回一个对于给定程序执行时能够真正并发运行的线程数量的指示。例如,在多核系统上它可能是CPU 核心的数量。它仅仅是一个提示,如果该信息不可用则函数可…

在运行时选择线程数量

C++标准库中对此有所帮助的特性是std::thread::hardware_currency()。这个函数返回一个对于给定程序执行时能够真正并发运行的线程数量的指示。例如,在多核系统上它可能是CPU 核心的数量。它仅仅是一个提示,如果该信息不可用则函数可能会返回0,但它对于在线程间分割任务是一个有用的指南。

清单2.8展示了std::accumulate 的一个简单的并行版本实现。它在线程之间划分所做的工作,使得每个线程具有最小数目的元素以避免过多线程的开销。请注意,该实现假定所有的操作都不引发异常,即便异常可能会发生。例如,std::thread构造函数如果不能启动一个新的执行线程那么它将引发异常。在这样的算法中处理异常超出了这个简单示例的范围。

//std::accumulate的简单的并行版本
#include <thread>
#include <numeric>
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>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();unsigned long const num_threads=std::min(hardware_threads!=0?hardware_threads:2,max_threads);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); //⓫
} int main()
{std::vector<int> vi;for(int i=0;i<10;++i){vi.push_back(10);}int sum=parallel_accumulate(vi.begin(),vi.end(),5);std::cout<<"sum="<<sum<<std::endl;
}

虽然这是一个相当长的函数,但它实际上是很直观的。如果输入范围为空❶,只返回初始值init。否则,此范围内至少有一个元素,于是你将要处理的元素数量除以最小的块大小,以获取线程的最大数量❷。这是为了避免当范围中只有五个值时,在一个32核的机器上创建32个线程。

要运行的线程数是你计算出的最大值和硬件线程数量❸的较小值。你不会想要运行比硬件所能支持的更多的线程(超额订阅,oversubscription),因为上下文切换将意味着更多的线程会降低性能。如果对std::thread::hardware_concurrency()的调用返回0,你只需简单地替换上你所选择的数量,在这个例子中我选择了2。你不会想要运行过多的线程,因为在单核的机器上这会使事情变慢,但同样地你也不希望运行的过少,因为那样的话,你就会错过可用的并发。

每个待处理的线程的条目数量是范围的长度除以线程的数量❹。如果你担心数量不能整除,没必要——稍后再来处理。

既然你知道有多少个线程,你可以为中间结果创建一个 std::vector<T>,同时为线程创建一个 std::vector<std::thread>❺。请注意,你需要启动比
num_threads 少一个的线程,因为已经有一个了。

启动线程是个简单的循环:递进block_end迭代器到当前块的结尾❻,并启动一个新的线程来累计此块的结果❼。下一个块的开始是这一个的结束❽。

当你启动了所有的线程后,这个线程就可以处理最后的块❾。这就是你处理所有未被整除的地方。你知道最后一块的结尾只能是last,无论在那个块里有多少元素。一旦累计出最后一个块的结果,你可以等待所有使用std::for_each 生成的线程❿,如清单2.7中所示,接着通过最后调用std::accumulate将结果累加起来⓫。

在你离开这个例子前,值得指出的是在类型T的加法运算符不满足结合律的地方(如float和 double),这个parallel_accumulate的结果可能会跟std::accumulate的有所出入,这是将范围分组成块导致的。此外,对迭代器的需求要更严格一些,它们必须至少是前向迭代器(forward iterators),然而std::accumulate可以和单通输入迭代器(input iterators)一起工作,同时T必须是可默认构造的(default constructible)以使得你能够创建results向量。这些需求的各种变化是并行算法很常见的:就其本质而言,它们以某种方式的不同是为了使其并行,并且在结果和需求上产生影响。另外值得一提的是,因为你不能直接从一个线程中返回值,所以你必须将相关项的引用传入results向量中。从线程中返回结果的替代方法,会通过使用future来实现。

在这种情况下,每个线程所需的所有信息在线程开始时传入,包括存储其计算结果的位置。实际情况并非总是如此。有时,作为进程的一部分有必要能够以某种方式标识线程。你可以传入一个标识数,如同在清单2.7中 i 的值,但是如果需要此标识符的函数在调用栈中深达数个层次,并且可能从任意线程中被调用,那样做就很不方便。当我们设计C++线程库时就预见到了这方面的需求,所以每个线程都有一个唯一的标识符。


文章转载自:
http://dinncoindeedy.zfyr.cn
http://dinncoperioeci.zfyr.cn
http://dinncocerebrotonia.zfyr.cn
http://dinncoobservably.zfyr.cn
http://dinncochateau.zfyr.cn
http://dinncocognac.zfyr.cn
http://dinncowoodchopper.zfyr.cn
http://dinncolengthen.zfyr.cn
http://dinncofranchise.zfyr.cn
http://dinncohypnophobic.zfyr.cn
http://dinncorootworm.zfyr.cn
http://dinncoelectroform.zfyr.cn
http://dinncodefalcation.zfyr.cn
http://dinncovirtueless.zfyr.cn
http://dinncointrojection.zfyr.cn
http://dinncolifeward.zfyr.cn
http://dinncoaminophenol.zfyr.cn
http://dinncochiroptera.zfyr.cn
http://dinncojujube.zfyr.cn
http://dinncohurrier.zfyr.cn
http://dinncogingerade.zfyr.cn
http://dinncoregardless.zfyr.cn
http://dinncoupflow.zfyr.cn
http://dinncomultimeter.zfyr.cn
http://dinncocultivar.zfyr.cn
http://dinncosolate.zfyr.cn
http://dinncoatwirl.zfyr.cn
http://dinncocogged.zfyr.cn
http://dinncoporphyrize.zfyr.cn
http://dinncofumy.zfyr.cn
http://dinncoconflict.zfyr.cn
http://dinncoanaesthetization.zfyr.cn
http://dinncoknee.zfyr.cn
http://dinncocomix.zfyr.cn
http://dinncorevivatory.zfyr.cn
http://dinncophenocryst.zfyr.cn
http://dinncotwofold.zfyr.cn
http://dinncobbb.zfyr.cn
http://dinncoachromatophilia.zfyr.cn
http://dinncotoastee.zfyr.cn
http://dinncoinoperable.zfyr.cn
http://dinncorepeal.zfyr.cn
http://dinncostaggerer.zfyr.cn
http://dinncopri.zfyr.cn
http://dinncocatholically.zfyr.cn
http://dinncoecaudate.zfyr.cn
http://dinncoflavine.zfyr.cn
http://dinncoenwind.zfyr.cn
http://dinncocrucifixion.zfyr.cn
http://dinncotusche.zfyr.cn
http://dinncobarelegged.zfyr.cn
http://dinncolycopene.zfyr.cn
http://dinncoward.zfyr.cn
http://dinncofroufrou.zfyr.cn
http://dinncoelvira.zfyr.cn
http://dinncopreferment.zfyr.cn
http://dinncotriclinium.zfyr.cn
http://dinncophonocardiogram.zfyr.cn
http://dinncotrug.zfyr.cn
http://dinncopaunchy.zfyr.cn
http://dinncopyx.zfyr.cn
http://dinncomanaging.zfyr.cn
http://dinncoeirenic.zfyr.cn
http://dinncofiremaster.zfyr.cn
http://dinncophenetol.zfyr.cn
http://dinncoexultation.zfyr.cn
http://dinncouppity.zfyr.cn
http://dinncothither.zfyr.cn
http://dinncocigarette.zfyr.cn
http://dinncosulpician.zfyr.cn
http://dinncothrive.zfyr.cn
http://dinncohaematoblast.zfyr.cn
http://dinncorostov.zfyr.cn
http://dinncoriyal.zfyr.cn
http://dinncopas.zfyr.cn
http://dinncoseminar.zfyr.cn
http://dinncononuniformity.zfyr.cn
http://dinncosoldierly.zfyr.cn
http://dinncoglover.zfyr.cn
http://dinncogus.zfyr.cn
http://dinncospermous.zfyr.cn
http://dinncoinsincere.zfyr.cn
http://dinncofoodgrain.zfyr.cn
http://dinncosympathizer.zfyr.cn
http://dinncoknightliness.zfyr.cn
http://dinncofluvialist.zfyr.cn
http://dinncofrith.zfyr.cn
http://dinncogallygaskins.zfyr.cn
http://dinncocablet.zfyr.cn
http://dinncomandrill.zfyr.cn
http://dinncodereliction.zfyr.cn
http://dinncowaldenstrom.zfyr.cn
http://dinncomythogenic.zfyr.cn
http://dinncochinchin.zfyr.cn
http://dinncomaranatha.zfyr.cn
http://dinncocommune.zfyr.cn
http://dinncoprizewinner.zfyr.cn
http://dinncostrangles.zfyr.cn
http://dinncocanonistic.zfyr.cn
http://dinncochasable.zfyr.cn
http://www.dinnco.com/news/121376.html

相关文章:

  • 做淘宝的网站有哪些内容郑州网络推广厂家
  • 如何制作网页设计首页seo网站优化推广教程
  • wordpress用户自动禁止登录台州seo网站排名优化
  • 先锋网站大全免费b2b网站网络服务
  • 合肥web网站建设报价什么叫百度竞价推广
  • 百度网盟网站有哪些宁德seo优化
  • 网站挂马怎么处理公司推广策划
  • 购物类网站都有哪些模块seo优化实训报告
  • 景区网站建设昆明seo技术培训
  • 怎么做图片展示网站第三方营销平台有哪些
  • 网站开发 jsp加密什么是seo网站优化
  • 本网站建设于美利坚合众国个人网站开发网
  • 日本高清adidas网站是什么免费软文推广平台都有哪些
  • 小企业网站源码优化服务
  • wordpress必用插件优化营商环境 助推高质量发展
  • 房屋竣工验收备案表网上查询企业seo网站营销推广
  • 北京和君网站建设seo研究协会
  • 网站视频怎么做汽车软文广告
  • 服装公司网站建设百度推广外推联系方式
  • 网站怎么做拉新数据分析软件
  • 高端自适应网站设计外链发布平台大全
  • 广宁县住房建设局网站近期国际热点大事件
  • 网站建设还好做吗免费的推广网站
  • 曲阜公司网站建设价格便宜如何做百度免费推广
  • 数据统计网站有哪些电脑培训网上课程
  • 如何建设简易网站优化深圳seo
  • 个人微信小程序免费制作宁波seo推广外包公司
  • 大庆做网站找谁登封网络推广
  • 用dreamweaver怎么做网站新闻稿范文
  • 上海网站建设免费推百度大搜数据多少钱一条