Checking whether the current thread owns a lock

后端 未结 10 624
攒了一身酷
攒了一身酷 2021-02-02 13:32

Suppose I have the following code:

public class SomeClass()
{
    private readonly object _lock = new object();

    public void SomeMethodA()
    {
        lock         


        
10条回答
  •  梦谈多话
    2021-02-02 14:18

    I have no experience whatsoever with .NET programming, but in my own code (Delphi) I have once implemented such assertions by having custom Acquire() and Release() methods in a critical section class, with a private member variable for the id of the thread having acquired the cs. After EnterCriticalSection() the return value of GetCurrentThreadID was written, and before LeaveCriticalSection() the field value was reset.

    But I don't know whether .NET allows a similar thing, or whether you could implement this in your program...

提交回复
热议问题