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 : -
#include <iostream>
-
#include <unistd.h>
-
#include <limits.h>
-
-
#define NTHREADS 400
-
-
using namespace std;
-
-
void *thread_function(void *);
-
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
-
int counter = 0;
-
-
main()
-
{
-
pthread_t thread_id[NTHREADS];
-
int i, j;
-
int foo = 0;
-
-
for(i=0; i < NTHREADS; i++)
-
pthread_create(&thread_id[i], NULL, thread_function, NULL);
-
-
for(j=0; j < NTHREADS; j++)
-
foo = pthread_join(thread_id[j], NULL);
-
-
printf("Final counter value: %d\n", counter);
-
}
-
-
void *thread_function(void *dummyPtr)
-
{
-
printf("Thread number %ld and counter number %d\n", pthread_self(), counter);
-
pthread_mutex_lock( &mutex1 );
-
counter++;
-
pthread_mutex_unlock( &mutex1 );
-
}
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 : - #include <iostream>
-
#include <unistd.h>
-
#include <limits.h>
-
-
#define NTHREADS 400
-
-
using namespace std;
-
-
void *thread_function(void *);
-
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
-
int counter = 0;
-
-
main()
-
{
-
pthread_t thread_id[NTHREADS];
-
int i, j;
-
int foo = 0;
-
-
for(i=0; i < NTHREADS; i++)
-
pthread_create(&thread_id[i], NULL, thread_function, NULL);
-
-
for(j=0; j < NTHREADS; j++)
-
foo = pthread_join(thread_id[j], NULL);
-
-
printf("Final counter value: %d\n", counter);
-
}
-
-
void *thread_function(void *dummyPtr)
-
{
-
printf("Thread number %ld and counter number %d\n", pthread_self(), counter);
-
pthread_mutex_lock( &mutex1 );
-
counter++;
-
pthread_mutex_unlock( &mutex1 );
-
}
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.
Please do not double post, confine your questions on the same topic (especially if they are the same question) to a single thread.
Thanks
Try creating your thread array on the heap. You may be running out of stack memory.
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
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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):
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |