- Example

An example of thread-local data.

We'll cover the following...

Example

Press + to interact
// threadLocal.cpp
#include <iostream>
#include <string>
#include <mutex>
#include <thread>
std::mutex coutMutex;
thread_local std::string s("hello from ");
void addThreadLocal(std::string const& s2){
s += s2;
// protect std::cout
std::lock_guard<std::mutex> guard(coutMutex);
std::cout << s << std::endl;
std::cout << "&s: " << &s << std::endl;
std::cout << std::endl;
}
int main(){
std::cout << std::endl;
std::thread t1(addThreadLocal,"t1");
std::thread t2(addThreadLocal,"t2");
std::thread t3(addThreadLocal,"t3");
std::thread t4(addThreadLocal,"t4");
t1.join();
t2.join();
t3.join();
t4.join();
}

Explanation

  • By using the keyword thread_local ...

Access this course and 1400+ top-rated courses and projects.