【问题标题】:Python Semaphore: I Need Negative Initial ValuePython信号量:我需要负初始值
【发布时间】:2016-06-25 04:16:46
【问题描述】:

Python 的信号量不支持负初始值。那么,我如何让一个线程等到其他 8 个线程完成某些操作?如果 Semophore 支持负初始值,我可以将其设置为 -8,然后让每个线程将该值递增 1,直到我们得到一个 0,这会解除对等待线程的阻塞。

我可以在临界区手动增加一个全局计数器,然后使用条件变量,但我想看看是否有其他建议。

【问题讨论】:

    标签: python multithreading operating-system synchronization semaphore


    【解决方案1】:

    答案肯定已经晚了,但对其他人来说可能会派上用场。

    如果你想等待 8 个不同的线程来做某事,你可以等待 8 次。 你用 0 初始化一个信号量

    s = threading.Semaphore(0)

    然后

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

    会做的。


    完整示例:

    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()
    

    可能的输出:

    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
    

    【讨论】:

    • 这只是挂起
    • Thomas,我留下了一个完整的例子。主线程应该挂起,直到信号量被释放的次数与它被获取的次数相同。有点奇怪的例子,因为这里可以使用 join,但它确实说明了如何使用信号量。
    【解决方案2】:

    对于任何进一步的读者:从 python3.2 开始,有 Barrier Objects 提供了一个简单的同步原语,供需要相互等待的固定数量的线程使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多