Tuesday 15 January 2013

.net - Mutex c#.how system recognize mutually exclusive codes -



.net - Mutex c#.how system recognize mutually exclusive codes -

i have method a() in project a

and method b() in project b

i need create both method mutually exclusive

at nowadays code follows

in project a

mutex mutex = new mutex(fale,"mt"); mutex.waitone(); //some lengthy code mutex.releasemutex();

in project b have

mutex mutex = new mutex(fale,"mt"); mutex.waitone(); //some different lengthy code mutex.releasemutex();

i see both function works mutually exclusive expected.

now have 2 questions here

one:i utilize name name “mt” in both methods. reason why 2 code became mutually exclusive.

is there different way 2 codes mutually exclusive if utilize different name?

two:suppose code on project goes in infinite loop how can start method in b?(how can avoid dead lock)

the way mutex work having 2 proceeses requesting ownership on same object, first 1 gets , sec must wait until first releases it. name scheme uses determine if they're "same" or not, using different name allow access both @ same time (since each 1 request ownership of own mutex, nobody else claims).

about sec question, infinite loop not deadlock, think it's more bug in first process else. in simple program, such thing error , programme crash. timeout might solve it, may create problem if there shared resource held (which reason why utilize mutually exclusive access in first place). take precautions in avoiding such bugs in first place, or if can legitimally happen, release mutex early.

c# .net mutex

No comments:

Post a Comment