为什么线程调用轻量级进程? [英] Why are threads called lightweight processes?

查看:123
本文介绍了为什么线程调用轻量级进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为大部分的开销已经通过其创建过程中实现线程是轻量级。

A thread is "lightweight" because most of the overhead has already been accomplished through the creation of its process.

我发现这个在教程之一。

I found this in one of the tutorials.

谁能细说什么究竟意味着什么?

Can somebody elaborate what it exactly means?

推荐答案

有人声称,线程是轻量级是 - 根据不同的平台 - 不一定可靠

The claim that threads are "lightweight" is - depending on the platform - not necessarily reliable.

这是操作系统线程,支持原生code,例如执行C写的。因此,它必须提供一个体面的大小的堆栈,通常单位为MB。所以,如果你开始1000年主题(也许在试图支持1000到您的服务器的并发连接),你将有1 GB的在你的进程的内存需求之前,你甚至开始做任何实际工作。

An operating system thread has to support the execution of native code, e.g. written in C. So it has to provide a decent-sized stack, usually measured in megabytes. So if you started 1000 threads (perhaps in an attempt to support 1000 simultaneous connections to your server) you would have a memory requirement of 1 GB in your process before you even start to do any real work.

这是高度可扩展的服务器,一个现实的问题,所以,如果他们是轻量级在所有他们不使用线程。他们把他们作为重量级的资源。他们可能不是在池中创建线程的数量有限,并且让他们采取工作项从一个队列中。

This is a real problem in highly scalable servers, so they don't use threads as if they were lightweight at all. They treat them as heavyweight resources. They might instead create a limited number of threads in a pool, and let them take work items from a queue.

由于这意味着该线程是长寿命和小数目,这可能是更好的使用过程来代替。这样,你得到的地址空间隔离,没有一个真正的问题与运行资源不足。

As this means that the threads are long-lived and small in number, it might be better to use processes instead. That way you get address space isolation and there isn't really an issue with running out of resources.

在总结:警惕代线的制造营销的说法。并行处理是伟大的(越来越这将是必不可少的),但是线程是实现它的一种方式。

In summary: be wary of "marketing" claims made on behalf of threads. Parallel processing is great (increasingly it's going to be essential), but threads are only one way of achieving it.

这篇关于为什么线程调用轻量级进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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