Skip to content

Instantly share code, notes, and snippets.

@i
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save i/ef663a264373e95c288e to your computer and use it in GitHub Desktop.

Select an option

Save i/ef663a264373e95c288e 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 joined on after
// they are all created. As a result, output from
// the workers will vary because there's no guarantee
// as to which workers will start or finish first.
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++) {
error = pthread_create(&thread_pool[i], NULL, &worker, (int*)i);
if (error) {
printf("Error creating phtread\n");
return error;
}
}
for (i = 0; i < THREAD_COUNT; 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