Domanda

I'm working with a class that manages a game's content and I'd like to know how I'm able to block calls to the dispose method without locking onto a shared dispose-lock? The worker methods themselves are thread-safe and can be executed in parallel, and that's why I don't want to lock those methods with a shared lock the dispose-method tries to acquire when it's fired.

public void DoSomething()
{
    lock (method1Lock) { /* ... */ }
}

public void Dispose()
{
    lock (method1Lock)
    lock (method2Lock)
    lock (methodXLock)
    {

    }
}

is not appropriate in this case as, as I previously mentioned, the methods (DoSomething in this case) can run in parallel.

È stato utile?

Soluzione

Use a ReaderWriterLock. Let the Method... code take the read side and the Dispose the write side (which is exclusive).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top