none
Mutex WaitOne(timeout) - what is a good way to arrive at a good timeout interval RRS feed

  • Question

  • Hi,

    I have implemented a mutex without a timeout. My understanding of a mutex with timeout, let's say 100 milliseconds, the thread will be blocked for this time, if the code block finishes before the timeout of 100ms, then that will be released anyway.But if the code block takes more than 100 ms, then it would be force it to exit out of the mutex. This means that I would actually need to know how long my code block would ideally need to execute, add some buffer and set the timeout accordingly. Basically, the timeout just safeguards a long running code block or an external factor that it not in the code block's control that is causing it to run slow an extraneous to the code block that it not in it's control.

    How do you arrive at a good timeout interval?

    Thanks



    Friday, March 27, 2015 6:33 PM

Answers

  • Ok, so this is the client. In this case the timeout should be set on the web service client, not on the mutex.

    As for how long the timeout needs to be - that entirely depends on what the web service is doing and network conditions. Sure, you could use a Stopwatch to meaure how long a web service call takes but you shouldn't take the average, you should take the maximum value. And that's probably not going to be enough either as you're probably the only the only user of the web service and that's not a very realistic scenario.

    In practice you'll probably use a reasonably large timeout such as 1 minute or even more and increase that for special cases that require a lot of server time.

    Friday, March 27, 2015 10:00 PM
    Moderator

All replies

  • "My understanding of a mutex with timeout, let's say 100 milliseconds, the thread will be blocked for this time"

    Nope, it will be blocked for at most 100ms in case the mutex isn't signaled first.

    "But if the code block takes more than 100 ms, then it would be force it to exit out of the mutex."

    Nope, the "code block" is never forced to exit.

    The wait timeout isn't about how long the thread which owns the mutex can run, it's about how long a thread that whishes to acquire the mutex ownership is willing to wait.

    "This means that I would actually need to know how long my code block would ideally need to execute, add some buffer and set the timeout accordingly."

    Before that you need to decide if you need a timeout at all. The cases where a timeout makes sense are somewhat rare and advanced.

    Friday, March 27, 2015 7:29 PM
    Moderator
  • On your comment -- "The wait timeout isn't about how long the thread which owns the mutex can run, it's about how long a thread that whishes to acquire the mutex ownership is willing to wait."

    Thanks for the above clarification, that really helped me understand how the timeout mutex worked.

    I wanted to include a timeout for a webservice call, to ensure if the call goes into a hung state due to external factors like network, the thread waiting in line to acquire the mutex does not wait forever. I think a safe approach to come up with a timeout would be to just do a Stopwatch start and end to get an average time it takes to execute the code block and then buffer it. Let me know if there is a better way to arrive at it.


    ihaveacsharpquestion

    Friday, March 27, 2015 8:35 PM
  • "I wanted to include a timeout for a webservice call, to ensure if the call goes into a hung state due to external factors like network, the thread waiting in line to acquire the mutex does not wait forever."

    Are you using the mutex in the web service or in the client that calls the web service? If it's in the client then it's a bit odd, why do you need to own the mutex while you call a web service?

    Friday, March 27, 2015 9:00 PM
    Moderator
  • I want to update a value based on the value returned from the call.

    Step1. Update value

    Step2. returnValue = WebServiceCall

    Step 3. Update returnValue


    ihaveacsharpquestion

    Friday, March 27, 2015 9:04 PM
  • Ok, so this is the client. In this case the timeout should be set on the web service client, not on the mutex.

    As for how long the timeout needs to be - that entirely depends on what the web service is doing and network conditions. Sure, you could use a Stopwatch to meaure how long a web service call takes but you shouldn't take the average, you should take the maximum value. And that's probably not going to be enough either as you're probably the only the only user of the web service and that's not a very realistic scenario.

    In practice you'll probably use a reasonably large timeout such as 1 minute or even more and increase that for special cases that require a lot of server time.

    Friday, March 27, 2015 10:00 PM
    Moderator