在线程和进程之间进行选择,这是可以预见的 [英] Selection between thread and process, which would be prefferable

查看:98
本文介绍了在线程和进程之间进行选择,这是可以预见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我们必须在下面提到的选择之间做出决定:



1.进程正在创建一些X个工作线程。这些工作线程将创建特定长度的日志文件。

2.创建X个进程并使用这些进程创建特定长度的日志文件。



哪种方式更好。



我尝试过的方法:



这个问题是针对基于windows的环境给出的。

I have come across a problem where we have to take a decision between below mentioned choices:

1.A process is creating some X number of worker threads.These worker thread would create log file of specific length.
2.Create X number of processes and create log files of specific length by using those processes.

Which would be better Approach.

What I have tried:

This problem is given for the windows based environment.

推荐答案

我会根据经验使用第一个解决方案,很多线程都不会更快多线程,因为开销正在减慢。伟大的但是您的线程正在处理的资源。当您在一个磁盘上写入时,即使两个进程可能比一个进程慢,但是当您获取并计算远程数据时,一定数量的线程可能会更好。考虑线程同步,如磁盘写入(一次写入和全部写入)或网络访问。



因此,您需要通过测试来微调线程数。重要:使用发布代码进行测试。



提示:为调整显示另一种方法而不是预期的情况编写干净且可重复使用的代码。
I would use the first solution by knowning from experience, that a lot of threads arent faster in multithreading, because the overhead is slowing down. The great "but" is what resources your threads are working on. When you write at one disk even two processes can be slower than one, but when you fetch and compute remote data a solid amount of thread could better. Think about thread syncronization like on disk writing (write once and all) or network access.

So you need to finetune the thread count by testing. Important: Test with release code.

Tip: write clean and reusable code for the case that the tuning shows another approach than expected.


假设您需要某种并行执行(至少在概念上),那么它很大程度上取决于不同任务之间的交互。需要的交互越多,我建议使用线程越多。另一方面,如果交互是最小的,我建议使用不同的过程。您知道线程通信更简单,开销更少,而单线程进程提供更简单的执行模型,并且至少在原则上可以更可靠(我的两分钱)。
Assuming you need some kind of parallel execution (at least conceptually) then it strongly depends on the interaction between the different tasks. The more interaction is required the more I would suggest to use threads. On the other hand, if the interaction is minimal I would suggest to use different processes. You know threads communicates more simply and with less overhead while single thread processes provide a simpler execution model and could be, at least in principle, more reliable (my two cents).


这篇关于在线程和进程之间进行选择,这是可以预见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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