Created: 2017-01-22 Sun 11:43
Threads are allowed to obtain the semaphore until the internal counter hits a specified value.
import threading max_connections = 10 semaphore = threading.BoundedSemaphore(max_connections) semaphore.acquire() # do all the codes semaphore.release()
A Re-entrant lock will not block if the thread that already owns it tries to obtain it again, but will block if another thread tries to obtain it.
lock = threading.Lock() lock.acquire() lock.acquire() # this will block in called by the owning thread lock = threading.RLock() lock.acquire() lock.acquire() # this won't block if called by the owning thread
The event represents an internal flag.
event = threading.Event() # a thread can wait for the flag to be set event.wait() # another thread can set or reset it event.set() event.clear()
A Thread can be made to block until a Condition is satisfied.
from threading import Lock from threading import Condition lock = Lock() cv = Condition(lock) cv.acquire() # block to wait for condition # do the work cv.notifyAll() # wake all threads waiting on this condition or notify(n) # wake one thread waiting on this condition cv.release() # release the condition
Pretty much how the internet, and all of computing works.
Buffers are, in their simplest implemntation, FIFO structures. Data goes in one end and comes out the other
circuler buffer gif: MuhannadAjjan 1/12/2015, en.wikipedia.org