Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Semaphore: I Need Negative Initial Value

Python's semaphore doesn't support negative initial values. How, then, do I make a thread wait until 8 other threads have done something? If Semophore supported negative initial values, I could have just set it to -8, and have each thread increment the value by 1, until we have a 0, which unblocks the waiting thread.

I can manually increment a global counter inside a critical section, then use a conditional variable, but I want to see if there are other suggestions.

like image 497
Jobs Avatar asked Dec 24 '22 04:12

Jobs


1 Answers

Surely it's late for an answer, but it can come handy to someone else.

If you want to wait for 8 different threads to do something, you can just wait 8 times. You initialize a semaphore in 0 with

s = threading.Semaphore(0)

and then

for _ in range(8):
    s.acquire()

will do the job.


Full example:

import threading
import time

NUM_THREADS = 4

s = threading.Semaphore(0)

def thread_function(i):
    print("start of thread", i)
    time.sleep(1)
    s.release()
    print("end of thread", i)

def main_thread():
    print("start of main thread")
    
    threads = [
        threading.Thread(target=thread_function, args=(i, ))
        for i
        in range(NUM_THREADS)
    ]
    
    [t.start() for t in threads]
    
    [s.acquire() for _ in range(NUM_THREADS)]
    
    print("end of main thread")

main_thread()

Possible output:

start of main thread                                                                                                                                                                        
start of thread 0                                                                                                                                                                           
start of thread 1                                                                                                                                                                           
start of thread 2                                                                                                                                                                           
start of thread 3                                                                                                                                                                           
end of thread 0                                                                                                                                                                             
end of thread 2                                                                                                                                                                             
end of thread 1                                                                                                                                                                             
end of thread 3                                                                                                                                                                             
end of main thread
like image 88
ffigari Avatar answered Dec 26 '22 19:12

ffigari