Skip to content

Instantly share code, notes, and snippets.

@TommyWu-fdgkhdkgh
Last active July 6, 2019 08:27
Show Gist options
  • Select an option

  • Save TommyWu-fdgkhdkgh/fa35fad354811a1241270657d028421e to your computer and use it in GitHub Desktop.

Select an option

Save TommyWu-fdgkhdkgh/fa35fad354811a1241270657d028421e to your computer and use it in GitHub Desktop.
semaphore mutex = 1;
int next_count = 0;//等待next的個數
semaphore next = 0;
int x_count = 0;//等待x_sem的個數
semaphore x_sem = 0;
/* monitor */
/* monitor start */
wait(mutex);
...
body of F
...
if(next_count > 0){
signal(next);
}else {
signal(mutex);
}
/* monitor end */
/* condition variable wait, x.wait() */
x_count++;
if(next_count > 0){
signal(next);
}else {
signal(mutex);
}
wait(x_sem);
x_count--;
/* condition variable signal, x.signal() */
if(x_count > 0){
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment