线程有多贵? [英] How expensive are threads?

查看:20
本文介绍了线程有多贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统本机线程有多贵?主机操作系统为线程堆栈分配一些虚拟内存,为线程控制结构分配一点内核内存.我错过了什么吗?

How expensive is a OS native thread ? The host OS allocates some virtual memory for a thread stack and a little bit of the kernel memory for the thread control structures. Am I missing something?

推荐答案

它可以增加调度器的工作量,这取决于线程的繁忙程度,以及调度器的类型.它还将为堆栈的第一页分配物理内存.

It can increase the scheduler workload, depending how busy the thread is, and the kind of scheduler. It will also allocate physical memory for the first page of the stack.

在许多情况下,主要成本是缓存污染.有太多的活动并发任务会降低性能,因为太多的线程共享的缓存太少,而且它们只是不断地相互推回主内存,这对线程来说比简单地进入睡眠状态更糟糕,因为睡眠会导致数百个周期的单次惩罚,而在单个时间片内多次检索主内存会导致类似的开销,并且还意味着相应地更多的上下文切换,因为在该时间片内完成的工作要少得多.

The main cost in many cases is cache pollution. Having too many active concurrent tasks kills performance because too many threads are sharing too little cache, and they just keep shoving each other back onto main memory, which is a far worse indignity for a thread to suffer than simply being put to sleep, since sleeping incurs a single penalty of several hundred cycles, while retrieving main memory incurs a similar overhead several times during a single time-slice, and also means proportionally more context-switching since much less work gets done during that time-slice.

这篇关于线程有多贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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