Thread: Mutex lock question

  1. #1
    Registered User
    Join Date
    May 2008

    Mutex lock question

    Hi all,
    I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation.

    Below is the summary of my code :


    In some cases I get into the situation where the thread is hung at performTask() function and never returns. When this happens the other threads which are waiting are blocked.

    Please let me how can I kill the thread which is hung at performTask() function. Apart from using timed Mutex, is there a way I can return from performTask() function after waiting for a specified time?.


  2. #2
    Registered User
    Join Date
    Sep 2004
    You should never kill a thread. There is usually a function to do so (pthread_kill for instance), but in my experience it will usually crash your program or cause some other undesired behavior.

    The simple answer to your question is that you should not lock around functions that take a long time to finish. Take a look at your performTask() function, and ask yourself why you need a mutex lock around it. Since I have no idea what performTask() actually does, I can't really give you much advice here.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    The real problem is probably that performTask is locking some other lock that a thread waiting is supposed to do, hence the deadlock. No, the real solution is to change your design to fix the problem in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    There are two possibilities, neither of which involves killing a process or using timed lock attempts:

    1. The function which is holding the mutex has a bug, and is either not releasing the mutex, or is stuck in an infinite loop. Fix your bug.

    2. The function which holds the mutex is naturally an extremely complex function, or one that legitimately takes a long time to finish. In this case, you should not be holding a contended mutex while performing this operation. Alter your design.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores Question
    By ch4 in forum C Programming
    Replies: 2
    Last Post: 01-02-2009, 01:32 PM
  2. A simple question about pthread_cond_wait()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 09-17-2008, 06:56 AM
  3. Quick mutex question
    By Thantos in forum Linux Programming
    Replies: 1
    Last Post: 02-21-2004, 07:10 PM
  4. Mutex - location of lock/unlock
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2003, 12:28 PM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

Tags for this Thread