Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are these thread-safe?

I attended an interview today in which the interviewer asked me the following question :

Is re-entrancy and mutual exclusion thread-safe ? Can you explain why ?

I am relatively new to concurrent programming and could not answer it.. But i said ...

Mutual exclusion is thread safe . But re-entrancy is not and that is the reason why we have re-entrant locks .

The interviewer moved on to the next question though to a different area ... I think i messed this one up ...

What is he expecting me to say when he asked me this ?

like image 288
Flash Avatar asked Sep 20 '10 13:09

Flash


1 Answers

Proper answer should be:

Yes they are implementation of Thread safety.

re-entrancy

Writing code in such a way that it can be partially executed by one task, reentered by another task, and then resumed from the original task. This requires the saving of state information in variables local to each task, usually on its stack, instead of in static or global variables.

one example

Mutual exclusion

Access to shared data is serialized using mechanisms that ensure only one thread reads or writes the shared data at any time. Great care is required if a piece of code accesses multiple shared pieces of data—problems include race conditions, deadlocks, livelocks, starvation, and various other ills enumerated in many operating systems textbooks.

one example

like image 102
jmj Avatar answered Oct 05 '22 11:10

jmj