Re: Max number of pthreads in one process?
Re: Max number of pthreads in one process?
- Subject: Re: Max number of pthreads in one process?
- From: Mike <email@hidden>
- Date: Mon, 19 May 2008 11:52:22 -0700
Army Research Lab wrote:
Hi all, just to see what would happen, I wrote up a small program that
spawns as many threads as it can, and then tries to cancel all of them
politely (see pasted code below). On my machine, I spawn 7051 threads
before pthread_create() returns EAGAIN. This part is OK, I'm happy with it;
its when I start canceling threads that I run into problems. Only the first
2559 threads are canceled. At that point, my main thread gets stuck in
pthread_join() waiting for the other threads to cancel. Looking at it with
Shark, I find I'm stuck in semaphore_wait_trap(). I don't have control over
this, and as near as I can tell, I don't have any race conditions. So,
where is the bug?
uname -a returns Darwin anvil.arl.army.mil 8.11.1 Darwin Kernel Version
8.11.1: Wed Oct 10 18:23:28 PDT 2007; root:xnu-792.25.20~1/RELEASE_I386 i386
i386. This system is a fully patched 10.4.11 system.
The code was compiled with gcc -Wall -Wextra -pedantic -std=c99 -ggdb
Thanks,
Cem Karan
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
pthread_mutex_t myLock = PTHREAD_MUTEX_INITIALIZER;
bool dieNow = false;
void *threadFunc(void *arg)
{
printf("About to start loop.\n");
while (true)
{
assert(pthread_mutex_lock(&myLock) == 0);
if (dieNow)
{
assert(pthread_mutex_unlock(&myLock) == 0);
break;
}
assert(pthread_mutex_unlock(&myLock) == 0);
usleep(100);
}
printf("Got message to quit.\n");
return NULL;
}
int main(void)
{
pthread_t ID[10000];
uintmax_t counter, counter2;
int result;
bool noMoreThreads = false;
bzero(ID, 10000 * sizeof(pthread_t));
for (counter = 0; counter < UINT_MAX; counter++)
{
result = pthread_create(&(ID[counter]), NULL, threadFunc, NULL);
switch (result)
{
case 0:
break;
case EAGAIN:
printf("Maximum number of threads is %" PRIuMAX "\n",
counter);
noMoreThreads = true;
break;
case EINVAL:
printf("How in the world did I get an EINVAL error???\n");
exit(1);
break;
default:
printf("Unknown return value! %i \n", result);
exit(1);
break;
}
if (noMoreThreads)
{
break;
}
}
sleep(1);
assert(pthread_mutex_lock(&myLock) == 0);
dieNow = true;
assert(pthread_mutex_unlock(&myLock) == 0);
for (counter2 = 0; counter2 < counter; counter2++)
{
printf("Quitting thread number %" PRIuMAX "\n", counter2);
assert(pthread_join(ID[counter2], NULL) == 0);
}
return 0;
}
I believe I read somewhere the max # of processes per thread on OS X is
256, although you may want to check that. I think I recall reading it
somewhere in the Multithreading Guide.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden