Python Semaphore: I Need Negative Initial Value
Asked Answered
D

2

5

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.

Deme answered 25/6, 2016 at 4:16 Comment(0)
C
7

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
Conductance answered 5/7, 2017 at 21:52 Comment(2)
this just hangsJockey
Thomas, I left a full example. The main thread should hang until the semaphore is released the same amount of times it was acquired. Kind of weird example since a join could be used here instead, but it does illustrate how to use the semaphore.Conductance
I
3

For any further readers: starting from python3.2 there are Barrier Objects which provides a simple synchronization primitive for use by a fixed number of threads that need to wait for each other.

Infringe answered 26/8, 2018 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.