cpp thread local

thread-local

When to use thread local?

When you want to store data that is unique to each thread, you can use thread local storage. This is useful when you want to store data that is global to a thread, but not global to the entire program. For example, you might want to store a counter that is unique to each thread, or a pointer to a resource that is unique to each thread.

Usually each thread uses thread local when there are multiple function calls in each thread and each function call needs to access the same data.

#include <iostream>
#include <thread>

void add(int n) {
    thread_local int count = 0;
    // static thread_local int count = 0; // 两种写法等价!
    count += n;
    // 休眠n秒,防止输出时数据交错(Mac会出现)
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout<<std::this_thread::get_id()<<":"<<count<<std::endl;
}

int main() {
    std::thread td[2];
    for (int i = 0; i < 2; i++) {
        td[i] = std::thread(add, i+1);
    }
    for (int i = 0; i < 2; i++) {
        td[i].join();
    }
    return 0;
}

Output:

23456242050624:1
23456239949376:2
#include <iostream>
#include <thread>

class A {
public:
    void dump() {
        std::cout<<id<<":"<<count<<std::endl;
    }
    std::thread::id id;
    static thread_local int count;
};
thread_local int A::count = 0;

void add(int n) {
    A a;
    a.id = std::this_thread::get_id();
    a.count += n;
    std::this_thread::sleep_for(std::chrono::seconds(n));
    a.dump();
    A aa;
    aa.dump(); // aa 和 a 中的count在同一个线程内相同。
}
int main()
{
    std::thread td[2];
    for (int i = 0; i < 2; i++) {
        td[i] = std::thread(add, i+1);
    }
    for (int i = 0; i < 2; i++) {
        td[i].join();
    }
    return 0;
}

Output:

23456242050624:1
thread::id of a non-executing thread:1
23456239949376:2
thread::id of a non-executing thread:2



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Learning-based memory allocation for C++ server workloads summary
  • my question:
  • Binary search algorithm variant
  • Docker Rocksdb build
  • Difference between Dockerfile and Docker Compose