Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe thread stack size?

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.

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?

like image 366
Lee Netherton Avatar asked Nov 11 '10 12:11

Lee Netherton


1 Answers

You shouldn't be creating anywhere near that many threads, and you definitely shouldn't be making a new thread to do a small amount of operations. You should create a new thread if and only if your existing thread(s) are fully saturated AND there are more available physical or logical cares to do work. That puts a hard limit on a reasonable current application at about 10 threads or so, even if you ran on a hexacore you'd only need 12 or so at max. Such a design is very flawed, will use a huge amount of process memory, and won't really improve performance.

As for the stack size, you can't really compute how much you need for an arbitrary thread, as it totally depends on the code run. However, in Visual Studio, the typical stack size is a few megabytes. You would have to post the entire code AND the resulting disassembly executed by the thread to know how much stack size to use. Just stick it at a couple of megabytes.

like image 184
Puppy Avatar answered Oct 13 '22 20:10

Puppy