threading.Condition vs threading.Event

前端 未结 2 1609
醉话见心
醉话见心 2021-01-31 01:58

I have yet to find a clear explanation of the differences between Condition and Event classes in the threading module. I

2条回答
  •  自闭症患者
    2021-01-31 02:44

    Simply put, you use a Condition when threads are interested in waiting for something to become true, and once its true, to have exclusive access to some shared resource.

    Whereas you use an Event when threads are just interested in waiting for something to become true.

    In essence, Condition is an abstracted Event + Lock, but it gets more interesting when you consider that you can have several different Conditions over the same underlying lock. Thus you could have different Conditions describing the state of the underlying resource meaning you can wake workers that are only interested in particular states of the shared resource.

提交回复
热议问题