Checking whether the current thread owns a lock

后端 未结 10 627
攒了一身酷
攒了一身酷 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:20

    OK, I now understand your problem. Conceptually I am now thinking of a Semaphore object. You can easily check the semaphore value (a value greater or equal than 0 means the resource is available). Also, this does not actually lock the resource that being done by incrementing or decrementing the semaphore value. The Semaphore object is present in the .NET framework but i did not use it so I cannot provide a proper example. If necessary I can build a simple example and post it here. Let me know.

    Alex.

    L.E. I am unsure now if you actually have control to the synchronization mechanisms in the application. If you only have access to an object that could be locked or not, my solution could prove (once again) of no use.

提交回复
热议问题