Skip to content

Instantly share code, notes, and snippets.

@i
Created September 30, 2014 05:57
Show Gist options
  • Save i/622c4c7649029428732e to your computer and use it in GitHub Desktop.
Save i/622c4c7649029428732e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pthread.h>
#define THREAD_COUNT 30
// Each thread will run an
// instance of this function
void *worker(void *arg) {
printf("I'm worker #%d\n", (int)arg);
return NULL;
}
// In this example, pthreads are created and joined
// on immediately. This will prevent any race conditions
// but also prevent any concurrency, so it's pointless.
int main(int argc, char *argv[]) {
int i, failure;
pthread_t thread_pool[THREAD_COUNT]; // Array of threads
for (i = 0; i < THREAD_COUNT; i++) {
failure = pthread_create(&thread_pool[i], NULL, &worker, (int*)i);
pthread_join(thread_pool[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment