thread-safety definition
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

thread-safety definition



hi all,

It looks like what everyone is trying to achieve here is a way for C++
authors to write thread-safe libraries. The major problem which seems to
plague this discussion is that different people assume different
definitions for thread-safety. Here is my take. Hopefully, without too
many factual errors...

Definition "thread safety"
--------------------------
A thread-safe library is a library which uses locks to protect data
which can be potentially accessed from different threads. This kind of
code is typically written with a trivial small OS-abstraction layer
which implements basic semaphore semantics (or another synchronization
primitive). This kind of library was never written to deal with
"cancelation" (not the POSIX cancelation primitive but, more generally,
externally-triggered death of the thread) and is not safe with regard to
either "inside" or any form of "outside" cancelation as defined below.

Definition "cancelation"
------------------------
All OSes provide a mean to stop the execution flow of a thread either
from the inside of the thread or from the outside.

1) "inside cancelation": This is basically ExitThread (win32 name). It
exists on all the platforms which support a form of threads or another I
know of. It semantics vary a lot from one platform to the other
unfortunatly. On win32, it will not invoke any thread-specific cleanup
handlers (neither C++ exceptions nor SEH are involved). On BeOS
(exit_thread), it will behave just like on windows. On POSIX
(pthread_exit) systems, it will invoke the thread-specific cancelation
handlers.

2) "outside cancelation": There are two kinds of "outside cancelation":

	2.1) "async cancelation": The OS removes the thread from its list of
tasks to schedule and does nothing to cleanup the thread ressources.
This is the most extreme useless feature of a thread library. BeOS and
win32 provide it. POSIX does not provide it.

	2.2) "defered cancelation": I know of only POSIX to implement this. The
canceled flag for the target thread is set and the thread cancelation
handlers are invoked whenever the thread reaches a cancelation point
(that is, it calls one of a set of specific library functions).

Definition "Posix thread-safety":
---------------------------------
A library is "posix thread-safe" if it is thread-safe and
defered-cancelation-safe.


I hope the above definitions will help.

regards,
Mathieu
-- 
Mathieu Lacage <mathieu.lacage@xxxxxxxxxxxxxxx>