Thread: Question about deadlocks

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Question about deadlocks

    So I just want to clarify this before attempting to do multithread program. If I were to have 2 threads run simutaneously. And both of them are going to access the same QUEUE which is going to be mutex locked. Since both of them are going to fill the Queue up. Would this cause a deadlock and I would need pthread conditions to avoid deadlocks or will the mutex function cause the other thread to just wait until it becomes unlocked?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, it would not cause a deadlock. You will not need condition variables, just a mutex.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    So just to be sure since one thread locks the queue to access it would hte other thread just be in a wait state until the queue is unlocked and available for the next thread

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    yes, the other thread would wait until the first thread unlocks the mutex.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A deadlock occurs when both threads are waiting on a lock held by other threads that potentially waits on something else. Example:
    There are two locks: A and B.
    There are two threads: A and B.
    Thread A executes and locks Lock A.
    Thread B executes and locks Lock B.
    Thread A executes and tries to lock B. But B is already locked by Thread B, so it falls into a waiting state.
    Thread B executes and tries to lock Lock A. But Lock A is already locked by Thread A, so it falls into a waiting state.
    Since neither state will ever unlock the locks that the respective threads are waiting for, nothing will ever happen. This situation is called a Deadlock.

    But in your case, you can see this situation will never happen. It should never happen if the multi-threaded programming is done right.
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In certain circumstances a thread can also deadlock with itself. But Yeah Elysia has described the classic deadlock.

    My question would be that if two threads a filling a queue up, where is this data going? Wont some thread taking data out of the queue also at some point?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM