pthread_barrier_t barrier = NULL;
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
static int serialThreadCount = 0;
void* func(void* arg)
{
assert(pthread_mutex_lock(&mx) == 0);
int result = pthread_barrier_wait(&barrier);
if (PTHREAD_BARRIER_SERIAL_THREAD == result) {
serialThreadCount++;
}
assert(pthread_mutex_unlock(&mx) == 0);
return arg;
}
int main()
{
pthread_t t1, t2;
void* ret;
serialThreadCount = 0;
assert(pthread_barrier_init(&barrier, NULL, 2) == 0);
assert(pthread_create(&t1, NULL, func, (void*)1) == 0);
assert(pthread_create(&t2, NULL, func, (void*)2) == 0);
printf("t1 and t2 created...\n");
assert(pthread_join(t1, &ret) == 0);
printf("t1 joined, retval %d\n", ret);
assert(pthread_join(t2, &ret) == 0);
printf("t2 joined, retval %d\n", ret);
assert(serialThreadCount == 1);
assert(pthread_barrier_destroy(&barrier) == 0);
assert(pthread_mutex_destroy(&mx) == 0);
return 0;
}
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
static int serialThreadCount = 0;
void* func(void* arg)
{
assert(pthread_mutex_lock(&mx) == 0);
int result = pthread_barrier_wait(&barrier);
if (PTHREAD_BARRIER_SERIAL_THREAD == result) {
serialThreadCount++;
}
assert(pthread_mutex_unlock(&mx) == 0);
return arg;
}
int main()
{
pthread_t t1, t2;
void* ret;
serialThreadCount = 0;
assert(pthread_barrier_init(&barrier, NULL, 2) == 0);
assert(pthread_create(&t1, NULL, func, (void*)1) == 0);
assert(pthread_create(&t2, NULL, func, (void*)2) == 0);
printf("t1 and t2 created...\n");
assert(pthread_join(t1, &ret) == 0);
printf("t1 joined, retval %d\n", ret);
assert(pthread_join(t2, &ret) == 0);
printf("t2 joined, retval %d\n", ret);
assert(serialThreadCount == 1);
assert(pthread_barrier_destroy(&barrier) == 0);
assert(pthread_mutex_destroy(&mx) == 0);
return 0;
}