安全线程堆栈大小? [英] Safe thread stack size?

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

问题描述

我写了一些code,它滋生了不少线程(约合512的那一刻,但可能在未来获得更高)。每个线程的唯一执行操作的量小,所以想要的开销,该线程放置在系统上被保持在最低

I am writing some code that spawns quite a few threads (about 512 at the moment, but that could get higher in the future). Each of the threads only performs a small amount of operations, so I want the overhead that the threads place on the system to be kept at a minimum.

我使用 pthread_attr_setstacksize()设置堆栈大小,我可以从 PTHREAD_STACK_MIN 。但我的问题是:是否可以安全使用 PTHREAD_STACK_MIN 的线程堆栈大小如何去计算我多少堆栈需要?在那里,我将需要添加到我的计算任何隐藏费用?

I am setting the stack size using pthread_attr_setstacksize(), and I can get the minimal allowed stack size from PTHREAD_STACK_MIN. But my question is: Is it safe to use PTHREAD_STACK_MIN for the thread stack size? How do I go about calculating how much stack I need? Are there any hidden overheads that I will need to add on to my calculation?

此外,是否有任何其他的技术,我可以用它来减少系统上的线程的负担?

Also, are there any other techniques I can use to reduce the threads' burden on the system?

推荐答案

减小线程堆栈大小不会减少开销(不是在CPU,内存使用或性能方面)。您在这方面唯一的限制是考虑到线程平台上的总可用的虚拟地址空间。

Reducing the thread stack size will not reduce overhead (not in terms of CPU, memory use or performance). Your only limit in this respect is the total available virtual address space given to threads on your platform.

我会使用默认的堆栈大小,直到一个平台presents问题,否则(如果发生的话)。然后最小化堆栈使用,如果当出现问题。然而,这些会导致真正的性能问题,因为你需要打起来堆,或设计线程依赖分配其他地方。

I would use the default stack size until a platform presents problems otherwise (if it happens at all). Then minimize stack usage if and when problems arise. However these will lead to real performance issues, as you'll need to hit up the heap, or devise thread-dependent allocation elsewhere.

隐藏费用可能包括:


  • VLA 在堆栈上大型阵列,如分配,的 的alloca() 或只是简单的静态大小自动数组。

  • code你不控制或没有意识到使用为模板,工厂类等,但因为你没有指定C ++这样的后果,这是不太可能是一个问题。

  • 进口$从图书馆头等等,这些可能的版本之间切换,并显著改变他们的筹码,甚至线程使用C $角

  • 递归。出现这种情况是由于以上几点也考虑的事情如的boost ::绑定,可变参数模板,疯狂的宏,然后使用缓冲区或大型物体在栈上只是一般的递归。

  • Allocation of large arrays on the stack, such as by VLA, alloca() or just plain statically sized automatic arrays.
  • Code you don't control or weren't aware of the consequences of using such as templates, factory classes etc. However given that you did not specify C++, this is less likely to be a problem.
  • Imported code from libraries headers etc. These may change between versions and significantly alter their stack, or even thread usage.
  • Recursion. This occurs due to the above points also, consider things like boost::bind, variadic templates, crazy macros, and then just general recursion using buffers or large objects on the stack.

您除了可以设置堆栈大小,操作线程优先级,并挂起和恢复他们的要求,这将有助于显著调度程序和系统响应能力。 Pthreads中,您可以设置争范围; LWP 和范围调度其性能特性有很大的不同。

You can in addition to setting the stack size, manipulate the thread priorities, and suspend and resume them as required, which will significantly assist the scheduler and system responsiveness. Pthreads allow you to set contention scope; LWP and in scope scheduling vary widely in their performance characteristics.

下面是一些有用的链接:

Here are some useful links:

  • Improving Performance through Threads
  • linux pthread_suspend

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

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