I have two threads sharing two resources. I use two mutexes to synchronise them. They are running at differenct prority.
To avoid race condition objetcs are aquired and released in same order. Now if somebody has coded the application like this.
In first thread,
something like c = A+B
In second thread,
something like c = A+B
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
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.
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?
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.
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...
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.