Chapter 4

Thread-Safe Programming

Abstract

The thread-safety issue for function calls is discussed: under what conditions does a library function provide the correct, expected service when asynchronously called by several threads? Random number generators are used as examples of functions that are not thread safe. The thread-specific storage tools proposed by different programming environments (OpenMP, TBB, C++11, Windows) are discussed. They enforce thread safety in functions that are not by nature thread safe. Best practices concerning the thread specific storage tools are reviewed at the end of the chapter.

Keywords

Thread safety

Thread local storage

4.1 Introduction

We learned in Chapter 2 that it is perfectly legitimate to have several ...

Get Shared Memory Application Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.