Sponsored Content
Full Discussion: problem with threads in C
Top Forums Programming problem with threads in C Post 302481944 by jim mcnamara on Monday 20th of December 2010 09:10:43 AM
Old 12-20-2010
Assume Linux, it works pretty much the same elsewhere as well. Each thread is a process that shares some address space with the parent process, and each thread usually has about 1MB of stack space by default. There is also an imposed process limit, say 4096. We are not going near that total.

1. 100 threads == 100MB of stack space, 1000 threads == 1GB of stack space. There is a process limit on stack size, which is charged to the parent process, see ulimit.

2. Every thread requires a context switch to "run". Depending on your system cpu architecture -
For a single cpu with one core, this means for each LWP (thread or lightweight process) to get cpu you have for 100 threads:
100 == 100 context switches requested every time a quantum (time slice) elapses. Say you have a 10ms quantum. That is 100 x 100 or 10000 context switches requested every second. You are spending a lot of time doing context switches.

For 1000 LWP's you have 1000 x 1000 or 1 million possible context switches per second.
When this happens, you spend all of your time in context switching and scheduling processes.

In practice a few dozen threads should be your maximum. If you get up in the 100+ range you are going to seriously degrade performance of your app and your system overall.

Consider a vmstat or top display that shows 1000 active processes, each one wanting the cpu. Everybody is waiting, almost nobody is doing anything else but waiting.

The only time this kind of thread overload becomes feasible is the case where you have 100's of cpu cores available. Clearly you do not.
This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

old threads

Neo, if I have an old thread that is a few months old, and a few pages back in the forum it was posted in, is it ok to 'bump' it back to the front? or, would you rather i deleted the old thread, and just create a new one? btw the thread has no replies. (2 Replies)
Discussion started by: norsk hedensk
2 Replies

2. UNIX for Dummies Questions & Answers

threads

i am tring to sort lots of data thats in many columns by just one column but, if I use sort +16 inputfile the column fluctuates because some of the rows have spaces etc within the text, so the end result is just a mess as it jumps around the columns depending whether it has spaces or not ....ie... (2 Replies)
Discussion started by: Gerry405
2 Replies

3. Solaris

threads

Hi all! 1)Is there a way to write a program that will work on both solaris and intel based machines. 2)How can I achive this for a program that creates and synchronizes three threads. Thank you. vij. (3 Replies)
Discussion started by: vijlak
3 Replies

4. UNIX for Advanced & Expert Users

How many threads do I use ?

Hi, I have a program that has two types of threads: 1) Reader threads 2) Worker Threads Readers: Their only job is to read files. They just read data from the files and put them into a buffer. They are obviously I/O intensive. Workers: These are CPU intensive. They do some computation... (5 Replies)
Discussion started by: the_learner
5 Replies

5. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

6. Programming

Threads help

Hello! I started studying studying about POSIX Threads a few days ago... so I am a little confused and I would appreciate some help! I isolated this code... and I wonder if I could use threads in it! #include <unistd.h> #endif #include <math.h> //#include "main.h" #include <sys/time.h>... (1 Reply)
Discussion started by: smurf2
1 Replies

7. Shell Programming and Scripting

Perl v5.8.5 Threads Problem

Hi Unix gurus, I am facing a threading problem in Perl. I have a worker thread in perl in which I am calling a shell script. The shell script echo's output to the Standard Output from time to time as it progresses. In the worker thread, I am unable to display the echo statement of shell... (1 Reply)
Discussion started by: som.nitk
1 Replies

8. HP-UX

%Internal DCE Threads problem (version CMA BL10+)

Hi, I have a module by the name gateway, and it core dumps and gives a cma_dump.log file which says: %Internal DCE Threads problem (version CMA BL10+), terminating execution. % Reason: dispatch: no available VP (uniprocessor) The current thread is 3 (address 0x40107c40) DECthreads... (0 Replies)
Discussion started by: vanz
0 Replies

9. Programming

Java Threads

Hi guys, I want to start studding about java threads. but the only book available on the market is "Java Threads" by O'Reilly... (0 Replies)
Discussion started by: majid.merkava
0 Replies

10. Programming

Problem with timely execution of threads...

Hi Guys, We are having one multi-threaded application. The scenario is as follows: --------------------------------- Client 1 | APP Server -------------- Client 2 ... (1 Reply)
Discussion started by: 14341
1 Replies
pthread_default_stacksize_np(3T)										  pthread_default_stacksize_np(3T)

NAME
pthread_default_stacksize_np() - change the default stacksize SYNOPSIS
PARAMETERS
new_size The new default stack size. old_size Pointer to where the old default stack size is returned. DESCRIPTION
The function allows an application to change the default value for the stacksize attribute. This function must be called before any threads have been created. The new default stack size is passed in the new_size parameter. If not NULL, the previous default stack size is returned in old_size. If new_size is zero, this function can be used (at any time) to query the current default stack size. On HP-UX, threads with default stack sizes are cached after they terminate. The next time a thread is created with a default stack size, a cached thread (and its stack) are reused. This can result in significant performance improvements for However, if the default stack size is not appropriate for an application, it cannot take advantage of this performance enhancement. By using the function, the threads library will change the default stack size so that it matches the applications needs. This allows the application to utilize the performance benefit of cached threads. RETURN VALUE
Upon successful completion, returns zero. Otherwise, an error number is returned to indicate the error (the variable is not set). ERRORS
If any of the following occur, the function returns the corresponding error number: [EINVAL] The value specified by new_size is less than [EPERM] The calling process has already created threads (this must be called before any threads are created). WARNINGS
If the environment variable is set with a valid value for default stack size, then it takes precedence over the invocation of interface. However, it returns the current default stack size in old_size. The maximum default stack size value that may be set through the environment variable is restricted to 10 MB; any value greater than 10 MB will be rounded down to 10 MB. Care should be taken while setting the default stack size limit since this could impact the maximum number of threads that can be created in a process. AUTHOR
was developed by HP. SEE ALSO
pthread_attr_getstacksize(3T), pthread_attr_setstacksize(3T). STANDARDS CONFORMANCE
Pthread Library pthread_default_stacksize_np(3T)
All times are GMT -4. The time now is 05:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy