Max number of pthreads in one process?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Thread-index: Aci54Mk7B4M2/CXUEd22gQAdT0T19A== Thread-topic: Max number of pthreads in one process? User-agent: Microsoft-Entourage/11.4.0.080122 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; } _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Army Research Lab