473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Pthreads limit

Hi all. I am trying to spawn four hundred pthreads and I am getting an error from pthread_join() with error code 0. It seems that when the 382nd thread tries to join, there's a segmentation fault. I am using gcc v 4.1.3 on ubuntu 7.10 (32 bit) on a Pentium 4 @ 2.8GHz and 512 MB of RAM. Program runs perfectly with NTHREADS < 380 and the values of _POSIX_THREAD_THREADS_MAX, etc. are all set to high numbers. Simple example code follows :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <limits.h>
  4.  
  5. #define NTHREADS 400
  6.  
  7. using namespace std;
  8.  
  9. void *thread_function(void *);
  10. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  11. int counter = 0;
  12.  
  13. main()
  14. {
  15.     pthread_t thread_id[NTHREADS];
  16.     int i, j;
  17.     int foo = 0;
  18.  
  19.     for(i=0; i < NTHREADS; i++)
  20.         pthread_create(&thread_id[i], NULL, thread_function, NULL);
  21.  
  22.     for(j=0; j < NTHREADS; j++)
  23.         foo = pthread_join(thread_id[j], NULL); 
  24.  
  25.         printf("Final counter value: %d\n", counter);
  26. }
  27.  
  28. void *thread_function(void *dummyPtr)
  29. {
  30.    printf("Thread number %ld and counter number %d\n", pthread_self(), counter);
  31.    pthread_mutex_lock( &mutex1 );
  32.    counter++;
  33.    pthread_mutex_unlock( &mutex1 );
  34. }
Oct 25 '07 #1
7 3826
Hi all. I am trying to spawn four hundred pthreads and I am getting an error from pthread_join() with error code 0. It seems that when the 382nd thread tries to join, there's a segmentation fault. I am using gcc v 4.1.3 on ubuntu 7.10 (32 bit) on a Pentium 4 @ 2.8GHz and 512 MB of RAM. Program runs perfectly with NTHREADS < 380 and the values of _POSIX_THREAD_THREADS_MAX, etc. are all set to high numbers. Simple example code follows :

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <limits.h>
  4.  
  5. #define NTHREADS 400
  6.  
  7. using namespace std;
  8.  
  9. void *thread_function(void *);
  10. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  11. int counter = 0;
  12.  
  13. main()
  14. {
  15.     pthread_t thread_id[NTHREADS];
  16.     int i, j;
  17.     int foo = 0;
  18.  
  19.     for(i=0; i < NTHREADS; i++)
  20.         pthread_create(&thread_id[i], NULL, thread_function, NULL);
  21.  
  22.     for(j=0; j < NTHREADS; j++)
  23.         foo = pthread_join(thread_id[j], NULL); 
  24.  
  25.         printf("Final counter value: %d\n", counter);
  26. }
  27.  
  28. void *thread_function(void *dummyPtr)
  29. {
  30.    printf("Thread number %ld and counter number %d\n", pthread_self(), counter);
  31.    pthread_mutex_lock( &mutex1 );
  32.    counter++;
  33.    pthread_mutex_unlock( &mutex1 );
  34. }
Oct 25 '07 #2
JosAH
11,448 Expert 8TB
Hi, 400 threads are quite a few threads; can't you use a threadpool where threads
are reused?

kind regards,

Jos

p.s I'll move your question where it belongs: in the C/C++ Forum section.
Oct 25 '07 #3
sicarie
4,677 Expert Mod 4TB
Please do not double post, confine your questions on the same topic (especially if they are the same question) to a single thread.

Thanks
Oct 25 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Try creating your thread array on the heap. You may be running out of stack memory.
Oct 25 '07 #5
JosAH
11,448 Expert 8TB
Try creating your thread array on the heap. You may be running out of stack memory.
That's most likely not it, i.e. a pthread_t structure isn't that large. More likely is
that the process runs out of memory because every new thread gets its own stack
which is one or a couple of MBs per thread.

kind regards,

Jos
Oct 25 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
More likely is
that the process runs out of memory because every new thread gets its own stack
which is one or a couple of MBs per thread.
There's no option to use the stack of the calling thread?
Oct 25 '07 #7
JosAH
11,448 Expert 8TB
There's no option to use the stack of the calling thread?
That'd be extremely difficult and extremely fine grained locking would be necessary
then. Better have each thread have its own thread; they won't get in each others'
way then.

kind regards,

Jos
Oct 25 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: john | last post by:
Hi, I've heard that the lack of performance of JAVA on HP-UX 11.0 is due to bad implementation of pthreads. It should be solved in HP-UX 11.11. Can anyone tell me if this is true ? thanks,...
0
by: Kevin Dahlhausen | last post by:
I've been playing with pySonic on unix. PySonic is pyrex wrapper around the fmod sound library. Fmod is distributed as a shared library that uses pthreads. I am getting a segfault when a...
5
by: Arnd Schroeter | last post by:
Hello! I am a c++ programmer under the os linux. I am using pthreads and i am wondering why i can only use satic methods of a class to create a thread of it. How can i change this, because it...
15
by: John David Ratliff | last post by:
Maybe someone here can help me. I want to use C++, but pthreads seems to be nonfunctional in it. The following test code I've written in C and C++. The C version works, while the C++ one does...
2
by: Jack | last post by:
hi all I would find a parallel sorting algorithms written with pthread programs, as a benchmark to run. Any pointing will be of help thankyou in advance
2
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory...
5
by: Ed L. | last post by:
Is this pthreads warning of any concern? gcc -O2 -fno-strict-aliasing -Wall -Wmissing-prototypes -Wmissing-declarations -pthread -pthreads -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS...
8
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): ...
4
by: Neel | last post by:
Hi, I 'm facing some prob with creating pthreads. My requirement is to create threads in a loop. but I dont want to make an array of pthread. At anytime I want to have 5 threads (I used a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.