多线程这将是最好用? (线程池或线程) [英] Multi threading which would be the best to use? (Threadpool or threads)

查看:197
本文介绍了多线程这将是最好用? (线程池或线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但愿这比我以前的一个更好的问题。我有一个.exe文件,我将通过不同的参数(文件路径),并向它然后采取并解析。因此,我将有一个循环下去,通过文件的路径列表中循环,并将其传递给该.exe文件。

Hopefully this is a better question than my previous. I have a .exe which I will be passing different parameters (file paths) to which it will then take in and parse. So I will have a loop going, looping through the file paths in a list and passing them to this .exe file.

有关这个更有效率,我想分散在多个内核,我想你通过线程做执行。

For this to be more efficient, I want to spread the execution across multiple cores which I think you do through threading.

我的问题是,我应该使用线程池,或多个线程运行此.EXE异步?

My question is, should I use the threadpool, or multiple threads to run this .exe asynchronously?

此外,这取决于其中的一个你们认为是最好的,如果你可以点我一个教程,将有什么,我想要做的一些信息。 !谢谢

Also, depending on which one of those you guys think is the best, if you can point me to a tutorial that will have some info on what I want to do. Thank you!

编辑:
我需要.exe文件的执行次数限制到一次执行每个核心。这是最有效的,因为如果我解析100,000个文件,我不能只火了100000的进程。所以我使用线程在同一时间限制处决的人数为每个核心一个执行。如果有另一种方式(比其他线程),以找出是否在执行的处理器没有太大的关系,或者如果.exe文件已完成,请解释一下。
,而如果没有另一种方式,我的最后一个问题是我怎么会用一个线程调用parse方法,然后再打当线程不再使用?

I need to limit the number of executions of the .exe to ONE execution PER CORE. This is the most efficient because if I am parsing 100,000 files I can't just fire up 100000 processes. So I am using threads to limit the number of executions at one time to one execution per core. If there is another way (other than threads) to find out if a processor isn't tied up in execution, or if the .exe has finished please explain. But if there isn't another way, my FINAL question is how would I use a thread to call a parse method and then call back when that thread is no longer in use?

第二次更新(非常重要):

我经历了什么每个人都告诉我,发现了一个关键元素我离开了,我认为并不重要。所以,我使用的是图形用户界面,我不希望它被锁定。这就是为什么我想用线程。我的主要现在的问题是,如何从一个线程发回的信息,所以我知道,当执行结束?

I went through what everyone told me, and found out a key element that I left out that I thought didn't matter. So I am using a GUI and I don't want it to be locked up. THAT is why I wanted to use threads. My main question now is, how do I send back information from a thread so I know when the execution is over?

推荐答案

由于我在回答你刚才的问题说,我觉得你不明白进程和线程之间的区别。过程是令人难以置信的重(*);每个进程可以包含多个线程。如果你是从父进程产卵新工艺,即父进程并不需要创建新的线程;每个进程都将拥有自己的线程集合。

As I said in my answer to your previous question, I think you don't understand the difference between processes and threads. Processes are incredibly "heavy" (*); each process can contain many threads. If you are spawning new processes from a parent process, that parent process doesn't need to create new threads; each process will have its own collection of threads.

只有创造,如果所有的工作都在同一个进程正在做父进程的线程。

Only create threads in the parent process if all the work is being done in the same process.

认为线程作为工人和处理作为含有一个或多个工人建筑物。

Think of a thread as a worker, and a process as a building containing one or more workers.

一个策略就是建立一个单一的建筑和十名工人谁做的每一个做一定量的工作来填充它。你得到建立一个进程和十个线程的代价。

One strategy is "build a single building and populate it with ten workers who do each do some amount of work". You get the expense of building one process and ten threads.

如果你的战略是建设一个建筑,然后在该大厦的顺序一个工人一千建设更多的建筑物,每一个都包含一个工人,做他们的竞价上网,那么你得到建设1001建筑和雇佣工人1001为代价。

If your strategy is "build a building. Then have the one worker in that building order the construction of a thousand more buildings, each of which contains a worker that does their bidding", then you get the expense of building 1001 buildings and hiring 1001 workers.

您做战略的的想追求的是建立一个建筑,聘请1000名工作者在该建筑物。然后指导每个工人建立一个建筑物,然后有一个工人去做真正的工作。有在做一个线程,其唯一的工作是创建一个过程,然后创建一个线程是没有意义的!你有1001的建筑和2001年的工人,其中一半是立即空闲,但仍必须支付

The strategy you do not want to pursue is "build a building. Hire 1000 workers in that building. Then instruct each worker to build a building, which then has one worker to go do the real work." There is no point in making a thread whose sole job is creating a process that then creates a thread! You have 1001 buildings and 2001 workers, half of whom are immediately idle but still have to be paid.

看你的具体问题:关键的问题是在哪里瓶颈?当性能问题在于PERF在处理器上控产卵关闭新工艺和新的线程不仅有助于。如果你的分析器的性能不选通你能多快解析该文件,而是你如何能迅速得到它的磁盘,然后并行它将会使事情的远,远差的。你将有一个巨大的,同时致力于所有锤在同一磁盘控制器上的系统资源量,以及磁盘控制器将获得更负荷堆积其上比较慢。

Looking at your specific problem: the key question is "where is the bottleneck?" Spawning off new processes or new threads only helps when the performance problem is that the perf is gated on the processor. If the performance of your parser is gated not on how fast you can parse the file but rather on how fast you can get it off disk, then parallelizing it is going to make things far, far worse. You'll have a huge amount of system resources devoted to all hammering on the same disk controller at the same time, and the disk controller will get slower as more load piles up on it.

我需要.exe文件的执行次数限制到一次执行每个核心。这是最有效的,因为如果我解析100,000个文件,我不能只火了100000的进程。所以我使用线程在同一时间限制处决的人数为每个核心一个执行。如果有另一种方式(比其他线程),以找出是否一个处理器没有在执行捆绑起来,或者如果.exe文件已经完成,请解释一下。

I need to limit the number of executions of the .exe to ONE execution PER CORE. This is the most efficient because if I am parsing 100,000 files I can't just fire up 100000 processes. So I am using threads to limit the number of executions at one time to one execution per core. If there is another way (other than threads) to find out if a processor isn't tied up in execution, or if the .exe has finished please explain

这似乎是一个非常复杂的方式去了解它。假设你有n个处理器。你提出的战略,按照我的理解,就是要火了n个线程,然后让每个线程火起来的一个进程,而且你知道,因为操作系统会的可能的每个CPU调度一个线程的不知何故的处理器将神奇地也安排的在每个新工艺的不同CPU上的新线程?

This seems like an awfully complicated way to go about it. Suppose you have n processors. Your proposed strategy, as I understand it, is to fire up n threads, then have each thread fire up one process, and you know that since the operating system will probably schedule one thread per CPU that somehow the processor will magically also schedule the new thread in each new process on a different CPU?

这似乎是依赖于操作系统的实现细节推理曲折链。这是疯狂。 如果您要设置一个特定进程的处理器关系,只是的设置过程处理器亲和力!不要做与线程这个疯狂的事情,并希望它的作品了。

That seems like a tortuous chain of reasoning that depends on implementation details of the operating system. This is craziness. If you want to set the processor affinity of a particular process, just set the processor affinity on the process! Don't be doing this crazy thing with threads and hope that it works out.

我说,如果你想拥有不超过一个可执行文件运行n个实例,每个处理器之一,周围的线程不乱的。相反,只是在一个循环中一个线程坐,不断地监测进程正在运行什么。如果有可执行文件运行少于n份,产生另一个和设置它的处理器亲和力是你最喜欢的CPU。如果有可执行文件运行n或更多的拷贝,还要在第二次(或一分钟,或任何有意义的)睡觉,当你醒来时,再次检查。继续做直到大功告成。这似乎是一个更容易的方法。

I say that if you want to have no more than n instances of an executable running, one per processor, don't mess around with threads at all. Rather, just have one thread sit in a loop, constantly monitoring what processes are running. If there are fewer than n copies of the executable running, spawn another and set its processor affinity to be the CPU you like best. If there are n or more copies of the executable running, go to sleep for a second (or a minute, or whatever makes sense), and when you wake up, check again. Keep doing that until you're done. That seems like a much easier approach.

(*)主题也是沉重的,但它们不是过程更轻。

(*) Threads are also heavy, but they are lighter than processes.

这篇关于多线程这将是最好用? (线程池或线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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