Skip to content

Instantly share code, notes, and snippets.

@albertzak
Created April 14, 2016 18:33
Show Gist options
  • Save albertzak/67bdffa95aa185e936365ecc6af928e6 to your computer and use it in GitHub Desktop.
Save albertzak/67bdffa95aa185e936365ecc6af928e6 to your computer and use it in GitHub Desktop.
int run[] = { 0, 0 };
int turn = 0;
/**
* Function to enter critical section.
*/
void enterCriticalSection_dekker(int threadId)
{
run[threadId] = 1;
while ( run[1 - threadId] != 0 ) {
if ( turn == (1 - threadId) ) {
run[threadId] = 0;
while ( turn == (1 - threadId) ) {}
run[threadId] = 1;
}
}
}
/**
* Function to leave critical section.
*/
void leaveCriticalSection_dekker(int threadId)
{
turn = (1 - threadId);
run[threadId] = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment