RoundRobin RoundRobin - 6 months ago 20
Linux Question

Threads , race condition and dead lock


  1. What does that fuction mean in Linux? Is the function thread self?

  2. Look at the picture, is there a possibility for a race condition or dead lock to occur?



enter image description here

Answer

tset here is probably a short form for test_and_set. Each thread tries to acquire both locks. If it succeeds, it increments the variables and releases the locks. Otherwise it gives up.

Looks like this is enough to avoid a race condition, assuming that tset is atomic.

Both threads use tset and release the lock immediately if they fail two acquire them. Therefore this code also is deadlock free.

However this does not mean that there are no problems with this code.

It is possible that the threads will keep acquiring and releasing locks without making any progress. This is situation is called livelock in concurrency theory.

If you have atomic test_and_set you could implement a proper critical section as Antti Haapala suggests in the comments.