我想了解一些关于并行编程的知识,所以我试图用一个简单的例子来实现Peterson的算法,其中一个共享计数器增加了2个线程。我知道彼得森不是最佳的,因为忙着等待,但我只是为了学习的原因才尝试过。Peterson算法的执行错误?
我认为这段代码的关键部分在线程函数add
中共享counter
递增。所以我在计数器增量之前调用enter_section
函数,之后我调用leave_function
。这部分是错的吗?我评估了关键部分是否错误?问题在于,当这两个线程完成时,计数器有时会给出不可预期的值。它必须是线程之间的同步问题,但我只是没有看到它......感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int counter; /* global shared counter */
int flag[2] = {0, 0}; /* Variables for Peterson's algorithm */
int turn = 0;
typedef struct threadArgs
{
pthread_t thr_ID;
int num_of_repeats;
int id;
} THREADARGS;
void enter_section (int thread) {
int other = 1 - thread;
flag[thread] = 1;
turn = thread;
while ((turn == thread) && (flag[other] == 1));
return;
}
void leave_section (int thread) {
flag[thread] = 0;
return;
}
void * add (void * arg) {
int i;
THREADARGS * a = (THREADARGS *) arg;
for (i = 0; i < a->num_of_repeats; i++) {
enter_section(a->id);
counter++;
leave_section(a->id);
}
return NULL;
}
int main() {
int i = 1;
pthread_attr_t thrAttr;
THREADARGS threadargs_array[2];
pthread_attr_init (&thrAttr);
pthread_attr_setdetachstate (&thrAttr, PTHREAD_CREATE_JOINABLE);
/* creating 1st thread */
threadargs_array[0].id = 0;
threadargs_array[0].num_of_repeats = 1000000;
pthread_create(&threadargs_array[0].thr_ID, &thrAttr, add, &threadargs_array[0]);
/* creating 2nd thread */
threadargs_array[1].id = 1;
threadargs_array[1].num_of_repeats = 2000000;
pthread_create(&threadargs_array[1].thr_ID, &thrAttr, add, &threadargs_array[1]);
/* free resources for thread attributes */
pthread_attr_destroy (&thrAttr);
/* waiting for 1st thread */
pthread_join (threadargs_array[0].thr_ID, NULL);
printf("First thread is done.\n");
/* waiting for 2nd thread */
pthread_join (threadargs_array[1].thr_ID, NULL);
printf("Second thread is done.\n");
printf("Counter value is: %d \n", counter);
return (EXIT_SUCCESS);
}
在http://bartoszmilewski.com/2008/11/05/who-ordered-memory-fences-on-an-x86/上有关于Peterson同步会发生什么的描述。虽然我可以发誓,但是'保护区'就足够了,另外,你需要编译器的屏障,我将它放在'mfence/sfence'上,并且在'while'循环的每一次迭代中。 – ninjalj 2012-02-27 19:49:20
如果你仍然对此感兴趣,可以参考http://stackoverflow.com/questions/11588514/a-tested-implementation-of-peterson-lock-algorithm/11656926#11656926 – ninjalj 2012-07-25 19:26:40