Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data.

When no sharing is intended, give each thread a private copy of the data. When sharing is important, provide explicit synchronization to make certain that the program behaves in a deterministic manner.

A procedure is thread safe when it is logically correct when executed simultaneously by several threads. At a practical level, it is convenient to recognize three levels of safety.

  • Unsafe
  • Thread safe – Serializable
  • Thread safe – MT-Safe

An unsafe procedure can be made thread safe and serializable by surrounding it with statements to lock and unlock a mutex. Example 6–1shows three simplified implementations of fputs(), initially thread unsafe.

Leave a Reply

Login with