CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2014
    Posts
    3

    Unlcoking a mutex from crashed thread

    In my mulithreaded C application, one of task got crashed while holding a mutex lock. This is now causing problem for other threads to execute with access to same semphores.
    I know in linux we have robust mutex to deal with it, but using this will require lots of changes in application(not possible). So can anybody tell me a way to unlock the mutex or solve the problem.
    As a solution I am thinking of deleting the locked mutex and recreating it for rest of the thread to use. I am not sure how safe is to implement this change.

    Thanks.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Unlcoking a mutex from crashed thread

    It is not clear wht do you mean here: task got crashed. If exception is unhandled, the whole program crashes. If exception is caught, here is the place to release the mutex.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Unlcoking a mutex from crashed thread

    This is the main reason to use RAII and why this makes C++ a better language than C to handle this sort of thing.

    For C. You'll need to unlock he mutex "somehow". This will probably involve quite a bit of nasty code to do so (pretty much simulating what a MutexLock object would do).

    Or the other solution. fix your code so it doesn't throw exceptions and allow the exceptions to remain unhandled.

  4. #4
    Join Date
    Oct 2014
    Posts
    3

    Re: Unlcoking a mutex from crashed thread

    Quote Originally Posted by Alex F View Post
    It is not clear wht do you mean here: task got crashed. If exception is unhandled, the whole program crashes. If exception is caught, here is the place to release the mutex.

    Sorry for not putting it in a clear way. By task I meant 'a thread'. it is a multithread application in C. Rest of the thread are getting stuck cause of forever locked mutex.
    I have no idea how to unlock a mutex which in lokced state due to a crashed thread.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Unlcoking a mutex from crashed thread

    Why is the thread crashing - and in particular why crashing when holding a mutex? How does another thread know that this thread has crashed - whether it's holding a mutex or not? When trying to obtain the mutex, don't you use a time-out interval so that threads waiting for the mutex are never stuck forever?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Unlcoking a mutex from crashed thread

    It is still not clear what do you mean by "crashed thread". Can you be more specific? Usually, when any thread in the process crashes, the whole process it terminated.

  7. #7
    Join Date
    Oct 2014
    Posts
    3

    Re: Unlcoking a mutex from crashed thread

    Quote Originally Posted by 2kaud View Post
    Why is the thread crashing - and in particular why crashing when holding a mutex? How does another thread know that this thread has crashed - whether it's holding a mutex or not? When trying to obtain the mutex, don't you use a time-out interval so that threads waiting for the mutex are never stuck forever?
    Actually these are VxWorks tasks that run independent to each other.
    We are not sure why task is crashing may be because of socket error, hardware failure or SSL issue. We are not able to find out the crash cause yet.

    The mutex guards a socket send routine, if one thread does't release it other thread wont't be able to use the socket access routine.
    we can not put a timeout in the mutex lock as it will skip the socket data send.

    In short it doesn't matter much if one thread or task is crashed, we are more concerned about locked resources(mutex here). How can we get it back for other thread to work?

    I am thinking of deleting and recreating the mutex(if owner task is in suspend state), would it be a good idea to do so ?

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Unlcoking a mutex from crashed thread

    Quote Originally Posted by Alex F View Post
    Usually, when any thread in the process crashes, the whole process it terminated.
    nope. not necessarily. if you want that behaviour like that, you need to code it that way.

    how your C++ framework handles uncaught C++ exceptions or how it handles uncaught hardware exceptions is implementation dependant.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Unlcoking a mutex from crashed thread

    What are you using to obtain the lock on the mutex? Are you checking its return code (e.g. WAIT_ABANDONED?)

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Unlcoking a mutex from crashed thread

    Quote Originally Posted by abhi009 View Post
    We are not able to find out the crash cause yet.
    so, I suppose that the thread is probably invoking undefined behavior somewhere ( otherwise, you should be able to easily find out where the error occurs ) ...

    Quote Originally Posted by abhi009 View Post
    In short it doesn't matter much if one thread or task is crashed, we are more concerned [...]
    are you sure ? being undefined behavior anything could happen, eg the crashing thread could have corrupted memory accessed by other threads ( including the data of any mutex release mechanism you come out with ) rendering your efforts moot. You have a bug, you should fix it.

    FYI, as other said, many aspects of exception handling ( especially on UB conditions ) are hardware and implementation dependent, so, what OS should this run on ? ( if on Windows, note that there's a dedicated subforum for that ... )
    Last edited by superbonzo; October 23rd, 2014 at 02:38 AM. Reason: added FYI

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured