Skip to content

Instantly share code, notes, and snippets.

@madbence
Last active March 10, 2016 19:58
Show Gist options
  • Select an option

  • Save madbence/f2d99e4acfac26bbd136 to your computer and use it in GitHub Desktop.

Select an option

Save madbence/f2d99e4acfac26bbd136 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* run(void *arg) {
printf("hello from %d!\n", *(int*)arg);
pthread_exit(NULL);
}
int main() {
int ids[4];
pthread_t threads[4];
for (int i = 0; i < 4; i++) {
ids[i] = i;
printf("create thread %d\n", i);
threads[i] = pthread_create(&threads[i], NULL, run, (void*)&ids[i]);
}
void* status;
for (int i = 0; i < 4; i++) {
printf("waiting for %d to join...\n", i);
pthread_join(threads[i], &status);
printf("thread %d joined with status %ld\n", i, (long)status);
}
pthread_exit(NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment