多少线程没有考虑到使他们成为一个坏的选择? [英] How many threads does it take to make them a bad choice?

查看:137
本文介绍了多少线程没有考虑到使他们成为一个坏的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写在C ++中不那么大的程序,使用boost ::线程。

I have to write a not-so-large program in C++, using boost::thread.

在眼前的问题是处理大量(也许几千几千或几万,数百millons是一种可能性以及)的(可能的)大文件数量。每个文件是独立于另一个,它们都驻留在同一目录中。使用多线程的形式给出的I'm思维,但问题是,有多少线程应该怎么用?我的意思是,大小什么顺序? 10,500,12400?

The problem at hand, is to process a large (maybe thousands or tens of thousands. Hundreds and millons are a possibility as well) number of (possibly) large files. Each file is independent from another, and they all reside in the same directory. I´m thinking of using the multi threaded aproach, but the question is, how many threads should I use? I mean, what order of magnitude? 10, 500, 12400?

有一些同步问题,每个线程应返回值(其被累积为每个文件)的一个结构,并且这些被加入到一个全局结构,以获得整体的数据。我知道有些线程可能因为同步的饿,但如果这只是一个附加的操作,有什么关系?

There are some synchronization issues, each thread should return a struct of values (which are accumulated for each file), and those are added to a "global" struct to get the overall data. I realize that some threads could "get hungry" because of synchronization, but if it's only an add operation, does it matter?

我在想

for(each file f in directory){

    if (N < max_threads)//N is a static variable controlling amount of threads
         thread_process(f)
    else
       sleep()
}

这是在HP - UX,但我不能经常测试它,因为它是一个遥远而相当不可访问服务器

This is in HP - UX, but I won't be able to test it often, since it's a remote and quite unaccessible server.

推荐答案

据阿姆达尔定律,是由香草萨特在的他的文章

According to Amdahl's law that was discussed by Herb Sutter in his article:

一个程序的处理中的某些量是充分的O(N)可并行(称之为部分P),并且只有该部分能够在具有越来越多处理器核的机器直接扩展。该计划的工作,剩下的就是O(1)连续(S)。 [1,2]假设完美的使用所有可用内核和并行无开销,Amdahl定律说,一个机器上的程序工作负载以N内核的最好的加速由
给出
  

Some amount of a program's processing is fully "O(N)" parallelizable (call this portion p), and only that portion can scale directly on machines having more and more processor cores. The rest of the program's work is "O(1)" sequential (s). [1,2] Assuming perfect use of all available cores and no parallelization overhead, Amdahl's Law says that the best possible speedup of that program workload on a machine with N cores is given by

在你的情况I / O操作可以采取的大部分时间,以及同步问题。你可以指望的时间,将在阻止(?)慢的I / O操作来​​花费约找到的线程数,这将是适合你的工作。

In your case I/O operations could take most of the time, as well as synchronization issues. You could count time that will be spend in blocking(?) slow I/O operations and approximately find number of threads that will be suitable for your task.


全部由香草萨特并发相关文章列表中可以发现<一个href=\"http://herbsutter.word$p$pss.com/2009/08/11/effective-concurrency-design-for-manycore-systems/\"相对=nofollow>这里。

Full list of concurrency related articles by Herb Sutter could be found here.

这篇关于多少线程没有考虑到使他们成为一个坏的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆