Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a mutex still need to be released if it times-out?

Using the WaitForSingleObject Function.

If the function is called and times out, does it still need to release the mutex?

i.e. should ReleaseMutex be in position 1. or 2. if the five seconds elapse?

WaitForSingleObject(5 second time out)
{
  //access shared resource

  //1. - ReleaseMutex() here?
}
  //2. - ReleaseMutex() here?
like image 502
T.T.T. Avatar asked Dec 27 '22 14:12

T.T.T.


2 Answers

No. If the call to WaitForSingleObject times out then you have not acquired the mutex, so should not release it.

i.e. you only need ReleaseMutex at position 1.

like image 187
Anthony Williams Avatar answered Jan 13 '23 13:01

Anthony Williams


Your case #1 is correct. If you time out on that call, it means the resource was not acquired and you should not attempt to release it.

like image 21
Amardeep AC9MF Avatar answered Jan 13 '23 11:01

Amardeep AC9MF