Should mutex.WaitOne() inside or before the try/finally block.

Posted by chillitom on Stack Overflow See other posts from Stack Overflow or by chillitom
Published on 2010-04-30T10:09:09Z Indexed on 2010/04/30 10:17 UTC
Read the original article Hit count: 256

Filed under:
|

Hi Guys,

I was wondering which of the following was the suggested pattern when using Mutex (or Semaphores or ReadWriteLockSlims etc.).

Should the initial lock happen inside or outside of the try statement? Is it unimportant?

_mutex.WaitOne()
try
{
 // critical code
}
finally
{
  _mutex.ReleaseMutex();
}

or

try
{
  _mutex.WaitOne()
 // critical code
}
finally
{
  _mutex.ReleaseMutex();
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about mutex