Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of a thread

I understand how to create a thread in my chosen language and I understand about mutexs, and the dangers of shared data e.t.c but I'm sure about how the O/S manages threads and the cost of each thread. I have a series of questions that all relate and the clearest way to show the limit of my understanding is probably via these questions.

What is the cost of spawning a thread? Is it worth even worrying about when designing software? One of the costs to creating a thread must be its own stack pointer and process counter, then space to copy all of the working registers to as it is moved on and off of a core by the scheduler, but what else?

Is the amount of stack available for one program split equally between threads of a process or on a first come first served?

Can I somehow check the hardware on start up (of the program) for number of cores. If I am running on a machine with N cores, should I keep the number of threads to N-1?

like image 364
Tommy Avatar asked Feb 12 '13 20:02

Tommy


2 Answers

then space to copy all of the working registeres to as it is moved on and off of a core by the scheduler, but what else?

One less evident cost is the strain imposed on the scheduler which may start to choke if it needs to juggle thousands of threads. The memory isn't really the issue. With the right tweaking you can get a "thread" to occupy very little memory, little more than its stack. This tweaking could be difficult (i.e. using clone(2) directly under linux etc) but it can be done.

Is the amount of stack available for one program split equally between threads of a process or on a first come first served

Each thread gets its own stack, and typically you can control its size.

If I am running on a machine with N cores, should I keep the number of threads to N-1

Checking the number of cores is easy, but environment-specific. However, limiting the number of threads to the number of cores only makes sense if your workload consists of CPU-intensive operations, with little I/O. If I/O is involved you may want to have many more threads than cores.

like image 130
cnicutar Avatar answered Oct 01 '22 02:10

cnicutar


You should be as thoughtful as possible in everything you design and implement.

I know that a Java thread stack takes up about 1MB each time you create a thread. , so they add up.

Threads make sense for asynchronous tasks that allow long-running activities to happen without preventing all other users/processes from making progress.

Threads are managed by the operating system. There are lots of schemes, all under the control of the operating system (e.g. round robin, first come first served, etc.)

It makes perfect sense to me to assign one thread per core for some activities (e.g. computationally intensive calculations, graphics, math, etc.), but that need not be the deciding factor. One app I develop uses roughly 100 active threads in production; it's not a 100 core machine.

like image 22
duffymo Avatar answered Oct 01 '22 00:10

duffymo