I work with 3 different threads (t0, t1 and t2), whom I need to synchronize behaviors this way:
My main thread will be creating these 3 threads, and those will be looping through prints and barrier just like I mentionned.
I tried to write the functions I pass them (f0 for t0, f1 for t1, etc.):
pthread_barrier_t b; //in my main I initialize pthread_barrier_init(&b, NULL, 3);
void *f0(void *arg){
while(1){
printf("A\n");
pthread_barrier_wait(&b);
pthread_barrier_wait(&b);
printf("D\n");
}
}
void *f1(void *arg){
while(1){
pthread_barrier_wait(&b);
printf("B\n");
pthread_barrier_wait(&b);
}
}
void *f2(void *arg){
while(1){
pthread_barrier_wait(&b);
printf("C\n");
pthread_barrier_wait(&b);
}
}
It worked well till the first barrier is met, but it looked like the barrier was not resetting, I tried to re-init once al threads came across it but it had weird effects.