multithreadinginpython icon indicating copy to clipboard operation
multithreadinginpython copied to clipboard

WaitGroup.add(count) inconsistency

Open griffrawk opened this issue 2 years ago • 0 comments

Allowing WaitGroup.add(count) to accept a value to be added to WaitGroup.wait_count seems to be inconsistent with WaitGroup.done() which only ever decrements the count by 1.

A thread could be started and the code erroneously adds a count > 1 to the WaitGroup by misusing the parameters. The main thread will never end (the WaitGroup.wait() continues to loop) because the child threads never reduce the count to zero. I've tried this in the concurrent file search example.

def file_search(root, filename, wait_group):
    print("Searching in:", root)
    for file in os.listdir(root):
        full_path = join(root, file)
        if filename in file:
            mutex.acquire()
            matches.append(full_path)
            mutex.release()
        if isdir(full_path):
            wait_group.add(2)
            t = Thread(target=file_search, args=([full_path, filename, wait_group]))
            t.start()
    wait_group.done()

Of course this is simply a programmer problem, unlikely to happen, but looks inconsistent for the example. Unfortunately this is baked into your video.

griffrawk avatar Dec 03 '22 13:12 griffrawk