Created
August 26, 2019 07:06
-
-
Save shelterz/4b13459668eec743f15be6c200aa91b2 to your computer and use it in GitHub Desktop.
pthread barrier usage example
This file contains 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_NUMS 4 | |
pthread_barrier_t barrier; | |
void *t0(void *param) | |
{ | |
pthread_barrier_wait(&barrier); | |
printf("t0 ready\n"); | |
} | |
void *t1(void *param) | |
{ | |
pthread_barrier_wait(&barrier); | |
printf("t1 ready\n"); | |
} | |
void *t2(void *param) | |
{ | |
pthread_barrier_wait(&barrier); | |
printf("t2 ready\n"); | |
} | |
int main(void) | |
{ | |
pthread_t t[3]; | |
pthread_barrier_init(&barrier, NULL, THREAD_NUMS); | |
pthread_create(&t[0], NULL, t0, NULL); | |
pthread_create(&t[1], NULL, t1, NULL); | |
pthread_create(&t[2], NULL, t2, NULL); | |
pthread_barrier_wait(&barrier); | |
printf("all sub threads ready, go!\n"); | |
pthread_barrier_destroy(&barrier); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this !