Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "free-threaded" and "thread-safe"

Sometimes I see the term "free-threaded" to describe a class or a method. It seems to have a similar or identical meaning to "thread-safe".

Is there a difference between the two terms?

like image 581
Stefan Avatar asked Oct 08 '10 15:10

Stefan


People also ask

What is thread-safe and not thread-safe?

Thread-safety is recommended when the web server run multiple threads of execution simultaneously for different requests. In Thread Safety binary can work in a multi-threaded web server context. Thread Safety works by creating a local storage copy in each thread so that the data will not collide with another thread.

What is considered thread-safe?

Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data. When no sharing is intended, give each thread a private copy of the data.

How do you know if something is thread-safe?

In general, when we say "a method is thread-safe" when there is no race-condition to the internal and external data structure of the object it belongs to. In other words, the order of the method calls are strictly enforced.

What does thread-safe function mean?

A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.


2 Answers

There may well be other things meant in other contexts, but in cases I've worked with in the past, "free threaded" means it works, or at least can work, across different threads without any marshalling between apartments.

Apartment-threading in contrast blocks off different "apartments" with separate copies of "global" data (which hence isn't really global, when you think about it) and either allows only one thread to operate in the apartment, or allows several but which will still be separate from those using other apartments.

Now, because the apartment model offers some thread-safety of its own, some (but not all) concerns about thread-safety go away. A piece of code that is designed to operate in an apartment model will be thread-safe, but some or all of that thread-safety is coming from the apartment model.

A free threaded piece of code will have to provide full guarantees of whatever degree of thread-safety it is claiming itself.

Which means it pretty much does mean the same thing as thread-safe, for any intents and purposes where you don't also have to consider the thread-safety of the use of apartment-model code.

like image 130
Jon Hanna Avatar answered Sep 28 '22 04:09

Jon Hanna


I've just done some research on what "free-threaded" might mean, and I also ended up with COM. Let me first cite two passages from the 1998 edition of Don Box' book Essential COM. (The book actually contains more sections about the free-threaded model, but I'll leave it at that for now.)


A thread executes in exactly one apartment at a time. Before a thread can use COM, it must first enter an apartment. […] COM defines two types of apartments: multithreaded apartments (MTAs) and singlethreaded apartments (STAs). Each process has at most one MTA; however, a process can contain multiple STAs. As their names imply, multiple threads can execute in an MTA concurrently, whereas only one thread can execute in an STA. […]

— from pages 200-201. (Emphasis added by me.)


Each CLSID in a DLL can have its own distinct ThreadingModel. […]

  • ThreadingModel="Both" indicates that the class can execute in either an MTA or an STA.
  • ThreadingModel="Free" indicates that the class can execute only in an MTA.
  • ThreadingModel="Apartment" indicates that the class can execute only in an STA.
  • The absence of a ThreadingModel value implies that the class can run only on the main STA. The main STA is defined as the first STA to be initialized in the process.

— from page 204. (Formatting and emphasis added by me.)


I take this to mean that a component (class) who is declared as free-threaded runs in an MTA, where concurrency of several threads is possible and calls to the component from different threads is explicitly allowed; ie. a free-threaded component supports a multithreaded environment. It would obviously have to be thread-safe in order to achieve this.

The opposite would be a component that is designed for an STA, ie. only allows calls from one particular thread. Such a class would not have to be thread-safe (because COM will take care that no other thread than the one who "entered" / set up the STA can use the component in the first place, that is, COM protects the component from concurrent access).

Conclusion: COM's term "free-threaded" essentially seems to have the same implications as the more general term "thread-safe".


P.S.: This answer assumes that "thread-safe" basically means something like, "can deal with concurrent accesses (possibly by different threads)."

P.P.S.: I do wonder whether "free-threaded" is the opposite of "having thread affinity".

like image 38
stakx - no longer contributing Avatar answered Sep 28 '22 03:09

stakx - no longer contributing