1

How to lock mutex (critical section) for some amount of time? Is there any API function for that? As far as I see there is nothing like that in windows. If so, is it possible to implement it manualy?

Thanks.

ledokol
  • 461
  • 1
  • 4
  • 13
  • 2
    Why would you want to do that? You should always strive to lock mutexes for the shortest amount of time possible (otherwise you're blocking other threads unnecessarily). Beyond that, unlocking a mutex before you're done with it will lead to horrible data corruption. – Adam Rosenfield Jan 19 '11 at 07:32
  • I'm talking about mutex lock timeout. Actually I'm interested in analogue of pthread_mutex_timedlock for windows. – ledokol Jan 19 '11 at 07:43
  • If you're blocking on a mutex that's never available and timing out, why don't you just use Sleep()? – Zak Jul 15 '12 at 01:44
  • Just because you can not imagine a reason for somebody to do something, doesn't mean there isn't a good reason to do it. The fact that there exists a posix function pthread_mutex_timedlock() means somebody thought it was worthwhile to build into the pthreads library. And windows has an implementation as well, so there must be something to it. – stu Aug 02 '16 at 20:55

1 Answers1

2

The equivalent of pthread_mutex_timedlock() in Win32 is using a Mutex and specifying a timeout (other than INFINITE) in one of the wait-functions.

Daniel Gehriger
  • 7,339
  • 2
  • 34
  • 55