Dekker algorithms fail under the multicore processor

Original link: http://www.cnblogs.com/lijingcheng/p/4454868.html

Dekker algorithm is a well-known concurrent programming algorithm, the core of the Dekker algorithm is a set of symmetrical code to access a set of shared variables, so that two threads can not simultaneously enter the critical section (as long as the cpu memory model is to follow the order of consistency a), so as to achieve thread synchronization. The following is an implementation of the algorithm:

static volatile int flag1 = 0;
static volatile int flag2 = 0;
static volatile int turn = 1;
static volatile int gSharedCounter = 0;

void dekker1( ) {
flag1 = 1;
turn = 2;
while((flag2 == 1) && (turn == 2)) ;
// Critical section
gSharedCounter++;
// Let the other task run
flag1 = 0;
}

void dekker2(void) {
flag2 = 1;
turn = 1;
while((flag1 == 1) && (turn == 1)) ;
// critical section
gSharedCounter++;
// leave critical section
flag2 = 0;
}

The key to the realization that while ((flag2 == 1) && (turn == 2)) and while ((flag1 == 1) && (turn == 1)) can never be established at the same time, allowing exclusive access critical area, and because the conditions can not be established at the same time, it will not lead to a deadlock. We were tested in the following example:

int gLoopCount;
void *task1(void *arg) {
int i;
printf("Starting task1n");
for(i=gLoopCount;i>0;i--) {
dekker1();
}
}
void *task2(void *arg) {
int i;
printf("Starting task2n");
for(i=gLoopCount;i>0;i--) {
dekker2();
}
}

In the single-core processor, multi-threaded way to execute the code, no matter how many times running, the program will not go wrong. Because the single-core CPU's memory model is to follow the order of consistency (Sequential Consistency) is, Sequential Consistency model (later referred to as SC), the simple fact that it is our impression that the program should have a multi-threaded execution order. However, the biggest problem SC is too low performance, because CPU / compiler totally unnecessary in the exact order (program order) specified in the code to execute each instruction. Studied architecture students should know whether the compiler CPU Ye Hao, doing what they do best is help you do out of order optimization. In the era of the disorder serial optimization is transparent to the programmer, a good package, you do not care about in the end they give you Chengshayang out of order, because they will ensure that the results of running the program optimized with you the program is expected to write the result is exactly the same. But after entering the multicore era, CPU and compiler optimization will continue to do those serial era, more importantly, these optimizations will break the SC model semantics your multithreaded programs, so that the actual operating results of our multi-threaded programs expected operating results to differ!
Take X86, its multi-processor memory model does not strictly enforce the SC, that is part of weak ordering (or called relax ordering?). It is only allowed to go out of order optimization (i.e., the store x-> load y scrambled optimized to load y -> store x) before the load may operate on different addresses mentioned store. And store x -> store y, load x -> load y, and the load y -> store x execution order of exchange is not permitted.
Therefore, for weak memory consistency of machines, the algorithm is likely to fail, because what order to update flag1 and flag2 is no limit for, in particular, can not guarantee flag2 read operation to occur in dekker1 in dekker2 after the write operation and turn the flag1.
dekkersbug.png

Reference: analysis of why the multi-core multi-threaded programs should be used with caution volatile keyword?

Reproduced in: https: //www.cnblogs.com/lijingcheng/p/4454868.html

Guess you like

Origin blog.csdn.net/weixin_30764883/article/details/94783179