Thread: Race condition

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Race condition

    Hello All,

    I have two threads sharing two resources. I use two mutexes to synchronise them. They are running at differenct prority.

    Code:
    In first thread,
    
    Acquire A
    
    Acquire B
    
    Use A 
    
    Use B
    
    something like c = A+B
    
    Release B
    
    Release A
    
    
    In second thread,
    
    Acquire B,
    
    Acquire A,
    
    use A
    
    Use B
    
    something like c = A+B
    
    Release A
    
    Release B
    To avoid race condition objetcs are aquired and released in same order. Now if somebody has coded the application like this.

    Race condition may occur or may not occur depending upon when this condition will occur. So we see application getting hung.

    Anybody knows test condition that tells for sure that race condition has occured and is because of these variables?

    Please let me know...

    This is not homework

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Because every use of variables A and B is protected, and c isn't a global variable, you shouldn't have a race condition. But you could have deadlock. In particular, one thread 1 could acquire A but not B, while thread 2 could acquire B but not A. Both threads would then be waiting for each other to relinguish their resources. Thread 1 would be waiting for B, and thread 2 would be waiting for A.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Ok, its deadlock.

    How do you develop test condition that will say for sure that hung condition is because of deadlock and is because of these variable?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you could do something like this:
    • Attempt to acquire resources
    • If unable to acquire all resources in X amount of time release all acquired resources
    • Wait Y amount of time
    • Repeat till all resources can be acquired
    • process resources
    • release resources


    At least an idea. You could also have a master thread that can poll the threads that are attempting to gain resources to find out what resources they have locked and which they are waiting for. This thread should be able to tell the other threads to release their resources.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Yes Thantos,

    I am trying to understand your idea. But here is what want to do. Any suggestion about this from you people are most appreciated.

    Like finding the memory leak in the application. I would like to stub acquire and release the resource and dump the resource name in file and delete from file when resource is released.

    Let me know, this will work or not...

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You should be able to by writing your own acquire and release #defines with __LINE__ and __FILE__ macros. You would then need a function that uses the OS support for time-out mutexes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Race Condition Help
    By Nor in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 07:43 PM
  3. Horse Race Program help
    By Bgamer90 in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2007, 10:49 PM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  5. Race condition: getting multiple threads to cooperate
    By FlyingDutchMan in forum C++ Programming
    Replies: 10
    Last Post: 03-31-2005, 05:53 AM