Are these threads safe?

Today I attended an interview 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 parallel programming and cannot answer it. But I said ...

Mutual exclusion is thread safe. But there is no re-entry, and it is for this reason that we have locks for re-entry.

The interviewer moved on to the next question, although in a different area ... I think I messed it up ...

What does he expect from me when he asked me about this?

+6
thread-safety concurrent-programming
source share
3 answers

The correct answer should be:

Yes, this is a Thread security implementation.

re-entrancy

Writing the code so that it can be partially completed by one task, re-entered by another task, and then resume the original task. This requires storing state information in variables local to each task, usually in its stack, and not in static or global variables.

one example

Mutual exclusion

Access to shared data is serialized using mechanisms to ensure that only one stream reads or writes to shared data at any time. Great care must be taken if a piece of code addresses several general data-related issues, including race conditions, deadlocks, livlocks, hunger, and various other diseases listed in many operating system textbooks.

one example

+5
source share

Both are thread safe - you can read it also on Wikipedia:
http://en.wikipedia.org/wiki/Reentrant_(subroutine)
http://en.wikipedia.org/wiki/Mutual_exclusion


Repeater mutexes are mutexes that can be locked multiple times from the same thread if there is a corresponding unlock for each lock.

0
source share

I quote http://en.wikipedia.org/wiki/Reentrant_(subroutine)

Both concepts of reentrant and thread safety relate to how functions handle resources. However, they do not match.

Although the concept of re-entry can affect the external interface of a function, thread safety only affects the implementation of the function, not its external interface.

- In most cases, in order to make a reentrant function without returning, its external interface must be changed so that all data is provided by the calling function.

- For a thread-safe function to be thread-safe, you only need to change the implementation, usually adding synchronization blocks to protect shared resources from simultaneous access on different threads.

Consequently, reconnection is a more fundamental property than thread safety and, by definition, leads to thread safety: each re-entry function is thread safe; however, not every thread-protected function is reentrant.

0
source share

All Articles