Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Last active December 7, 2019 11:33
Show Gist options
  • Save anhnguyen1618/8bd150896f07f595f0c123c4e2563e29 to your computer and use it in GitHub Desktop.
Save anhnguyen1618/8bd150896f07f595f0c123c4e2563e29 to your computer and use it in GitHub Desktop.
Class Monitor {
string[] states;
cond[] q
pickup() {
states[i] = 'Hungry'
while (check(i) == 'Hungry') {
queue[i].wait()
}
}
drop() {
states[i] = 'hungry'
queue[(i + 1) % 5].signal()
queue[(i + 4) % 5].signal()
}
check(i) {
if (
states[(i + 4) % 5] != 'eating' &&
states[(i + 1) % 5] != 'eating') {
states[i] = 'eating'
return 'eating'
}
return 'hungry'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment