Essential Tools Module User's Guide : Chapter 2 Class Overview : Common Functionality Among Classes : Multithread Safety
Multithread Safety
Rogue Wave defines three levels of thread safety, depending on how a class is implemented. These include unsafe, safe, and multithread-safe, as described below.
Unsafe (MT-0) -- An MT-0 function, class, or library is not safe to use in a multithreaded application unless the application arranges for only one thread at a time to access or execute within that function, class or library. Unsafe functions, classes or libraries often contain either global or static data that is not protected, or make use of functions or classes that are not safe.
Safe (MT-1) -- MT-1 code is reentrant. An MT-1 function, class, or library can be used in a multithreaded application, but may not be safely accessed by more than one thread at a time. If the code has any undocumented or private shared, global, or static data, it will automatically guard that data even across thread boundaries, so its behavior will be as expected: class instances will "act like an int" even in a multithreaded environment. Of course, if you share MT-1 objects between threads, you are responsible for avoiding race conditions.
MT-Safe (MT-2) -- An MT-2 function, class, or library is fully prepared for multithreaded access and execution. MT-2 objects are reentrant, protect their internal global or static data, and ensure that methods that access member data are protected. An MT-2 class or library implies that individual operations can be performed safely without external user locking and unlocking. However, an MT-2 class or library might still require the user to perform external synchronization or locking in situations where several individual operations must be combined and treated as a single atomic operation (for example, testing for, and reading the contents of a queue).
Assigning Thread Safety Levels to Classes and Libraries
When a class or library is assigned a thread safety level, it is possible that one or more individual members will differ from the classification of the whole. In the event that a member of a class or library is less thread-safe than its parent, this difference will be clearly documented. In addition, we’ll make every effort to document any thread-safe exceptions in a summary discussion of the class or library.
Thread Safety in the Essential Tools Module
Unless otherwise specified, all classes in the Essential Tools Module are assigned a thread safety level of MT-1. All of these classes perform sufficient internal locking on global and static data to ensure that they behave correctly when used in a multithreaded environment; however they also require that the user provide external locking around operations on objects that are shared between multiple threads.