Monitor.Enter and Monitor.Exit in different threads

前端 未结 3 355
鱼传尺愫
鱼传尺愫 2020-12-11 16:21

Monitor.Enter and Monitor.Exit are designed to be called from the same thread. But, what if I need to release a lock in a different thread than acq

相关标签:
3条回答
  • 2020-12-11 16:52

    Try using a ManualResetEvent, it's used to block thead(s) until some external event has been triggered. MSDN Doc:

    http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx

    0 讨论(0)
  • 2020-12-11 17:01

    If you can use .NET 4.0 you can replace it with System.Threading.Semaphore which allows you to acquire permits in one thread and release them in another.

    The Semaphore class does not enforce thread identity on calls to WaitOne or Release.

    0 讨论(0)
  • 2020-12-11 17:11

    The primitive you're looking for is called a semaphore which can be safely entered on one thread and exited from another.

    0 讨论(0)
提交回复
热议问题