Thread safety via TSD 

Prior to the TLS technique that we just saw (that is, before Linux 2.6 and gcc 3.3), how did one guarantee writing a new API to be thread safe? A much older technology exists, called TSD.

In a nutshell, TSD is a more complex solution from the application developer's viewpoint—more work must be done to achieve the very same end result that TLS so easily gives us; that of making a function thread-safe.

With TSD, the thread-safe routine must invoke an initializer function (usually done with pthread_once(3)), which creates a unique thread-specific data key (using the pthread_key_create(3) API). This initializer routine associates a thread-specific data variable (such as the iobuf buffer pointer in our example) with that key, using the pthread_getspecific(3) and pthread_setspecific(3) APIs. The end result is that the data item is now thread-specific and therefore thread-safe. Here, we do not delve further into using TSD as it's an older solution that TLS easily and elegantly replaces on modern Linux platforms. Nevertheless, for the interested reader, please refer to the Further reading section on the GitHub repository—we provide a link to using TSD.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset