Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Created December 6, 2019 14:10
Show Gist options
  • Save anhnguyen1618/64dddffd019fde4b49ab2f439ee90eec to your computer and use it in GitHub Desktop.
Save anhnguyen1618/64dddffd019fde4b49ab2f439ee90eec to your computer and use it in GitHub Desktop.
Semaphore Capacity = N
Semaphore hasItem = 0
Semaphore mutex = 1
Semaphore mutex_out = 1
Producer {
while (True) {
item = produce()
Capacity.acquire()
mutex.acquire()
in = (in + 1) mod N
buffer[in] = item
mutex.release()
hasItem.release()
}
}
Consumer {
while(True) {
hasItem.acquire()
mutex_out.acquire()
out = (out + 1) mod N
item = buffer[out]
mutex_out.release()
Capacity.release()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment