how can i do in C# that my function will be guarded by mutex semaphore a.k.a synchronize function in JAVA
There's no good way to do this, except to do it yourself:
private readonly object _locker = new object();
public void MyMethod()
{
lock (_locker) {
// Do something
}
}
You don't want synchronize functions like Java - they're a bad idea because they use a lock construct which other can interfere with. What you want is a lock object. Basically, in the class you want to protect, create a private member variable of type object
private readonly object lock_ = new object();
Then in any method you need to synchronize, use the lock construct to enter and leave the lock automatically:-
public void SomeMethod()
{
lock(lock_)
{
// ...... Do Stuff .........
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With