理解 Memory barrier(内存屏障)【转】 (3)

assert(!(r1 == 0 && r2 == 0));

}

 

void run1()

{

x = 1;

r1 = y;

}

 

void run2()

{

y = 1;

r2 = x;

}

 

// -------------------

static pthread_barrier_t barrier_start;

static pthread_barrier_t barrier_end;

 

static void* thread1(void*)

{

while (1) {

pthread_barrier_wait(&barrier_start);

run1();

pthread_barrier_wait(&barrier_end);

}

 

return NULL;

}

 

static void* thread2(void*)

{

while (1) {

pthread_barrier_wait(&barrier_start);

run2();

pthread_barrier_wait(&barrier_end);

}

 

return NULL;

}

 

int main()

{

assert(pthread_barrier_init(&barrier_start, NULL, 3) == 0);

assert(pthread_barrier_init(&barrier_end, NULL, 3) == 0);

 

pthread_t t1;

pthread_t t2;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsdzs.html