Last active
August 29, 2015 14:07
-
-
Save i/ef663a264373e95c288e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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