Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created September 3, 2009 14:08
Show Gist options
  • Select an option

  • Save jasonmadigan/180316 to your computer and use it in GitHub Desktop.

Select an option

Save jasonmadigan/180316 to your computer and use it in GitHub Desktop.
Find thread limit
/* gcc -lpthread -o thread-limit thread-limit.c */
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_THREADS 10000
int i;
void run(void) {
char c;
if (i < 10)
printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
sleep(60 * 60);
}
int main(int argc, char *argv[]) {
int rc = 0;
pthread_t thread[MAX_THREADS];
printf("Creating threads ...\n");
for (i = 0; i < MAX_THREADS && rc == 0; i++) {
rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
if (rc == 0) {
pthread_detach(thread[i]);
if ((i + 1) % 100 == 0)
printf("%i threads so far ...\n", i + 1);
}
else
printf("Failed with return code %i creating thread %i.\n",
rc, i + 1);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment