Don't understand the need for Monitor.Pulse()

前端 未结 3 621
广开言路
广开言路 2021-02-05 05:32

According to MSDN, Monitor.Wait():

Releases the lock on an object and blocks the current thread until it reacquires the lock.

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 05:48

    You use Enter / Exit to acquire exclusive access to a lock.

    You use Wait / Pulse to allow co-operative notification: I want to wait for something to occur, so I enter the lock and call Wait; the notifying code will enter the lock and call Pulse.

    The two schemes are related, but they're not trying to accomplish the same thing.

    Consider how you'd implement a producer/consumer queue where the consumer can say "Wake me up when you've got an item for me to consume" without something like this.

提交回复
热议问题