Thread-local Storage

Learn about the thread-local storage concurrency pattern.

Thread-local storage refers to the data that are specific to the thread. The data will seem like global static storage, but in essence, each copy will be created for each thread at the time of thread creation.

Essentially, thread-local storage enables a global state within a thread.

Typical use cases

Here are the three common use cases where we could use thread-local storage:

  • Porting a single thread to a multithreaded program.
  • Compute thread-local and share the results.
  • Thread-local logger.


Our plan is to fill a std::vector with one hundred million arbitrary numbers between 1 and 10. We will apply the uniform distribution to get the numbers. And at the end, we will have to calculate the sum of all values.

Before showing the example of thread-local storage, let’s take a look at the code before we use thread-local storage.

Get hands-on with 1200+ tech skills courses.