Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Re-entrant and Thread-Safe function

What is the difference between a re-entrant function and a thread safe function?

like image 891
Jay Avatar asked Feb 17 '10 06:02

Jay


1 Answers

  • A thread-safe function can be called simultaneously from multiple threads, even when the invocations use shared data, because all references to the shared data are serialized.

  • A reentrant function can also be called simultaneously from multiple threads, but only if each invocation uses its own data.

Hence, a thread-safe function is always reentrant, but a reentrant function is not always thread-safe.

The difference can be cottoned on with the example,

A class is said to be reentrant if its member functions can be called safely from multiple threads, as long as each thread uses a different instance of the class. The class is thread-safe if its member functions can be called safely from multiple threads, even if all the threads use the same instance of the class.

Source: Qt

like image 120
snr Avatar answered Oct 07 '22 06:10

snr