Data that is private to a thread

You can use two methods to create data that is private to a thread: thread local storage and thread-specific data.

Thread local storage

Thread local storage is similar to thread-specific storage, except that thread local storage is implemented by the operating system, whereas thread-specific storage is implemented with APIs. As a result, thread local storage is faster and easier to use than thread-specific storage.

ILE C, ILE C++, and ILE RPG compilers support thread local storage.

Thread-specific data

Thread-specific data allows a thread to maintain its own global storage that is hidden from the other threads.

When changing the application or application services to run in a multithreaded application, you must use a synchronization technique to protect global storage from being changed by multiple threads at the same time.

Due to the design of the application, threads might not function correctly if they share the global storage of the application. If eliminating the global storage is not feasible, you should consider using thread-specific data.

Consider the example of a server that stores information about the client and the current transaction in global storage. This server is never able to share the client information in a multithreaded environment without significant redesign. The application can instead pass the client information from function to function instead of using the global client information.

However, the application can be modified to use thread-specific data more easily than it can be modified to eliminate the use of global storage. When each new thread is created, the thread uses a global identifier (or key) to create and store its thread-specific data. Each client (thread) then has unique but global client data.

In addition, some APIs provide a way for the system to automatically call a data destructor function that cleans up the thread-specific data when a thread ends.