c - Critical section example - Does it work? -
me , friends came across example code online , arguing wether implements critical section or not.. our opinions going , forth thought asking stackoverflow.
so, implement critical section here or not?
int flagga[2]; void task0(void){ while(1){ /* code */ flagga[0]=true; while(flagga[1]==true) /*do nothing*/; /* critical sektion */ flagga[0]=false; } } void task1(void){ while(1){ /* code */ flagga[1]=true; while(flagga[0]==true) /*do nothing*/; /* critical section */ flagga[1]=false; } } void main(void){ flagga[0]=flagga[1]=false; startthread(task0); startthread(task1); while(1); }
it suffers possible starvation: if neither task gets inner while
before other has set flagga
true (which can happen if stickily alternate between statements in each task), both stuck in inner while loop.
Comments
Post a Comment