murphypei / murphypei.github.io

15 stars 6 forks source link

C++11 thread_local 用法用例中关于类对象的最后一个例子输出结果错误 #83

Open wangpeipei90 opened 2 years ago

wangpeipei90 commented 2 years ago
void loopin_func(const std::string& thread_name) {
    thread_local A* a;
    a = creatA();
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "thread[" << thread_name << "]: a.counter:" << a->get_value() << std::endl;
    return;
}

对应的输出应为

create A
thread[t1]: a.counter:0
create A
thread[t1]: a.counter:0
create A
thread[t1]: a.counter:0
create A
thread[t2]: a.counter:0
create A
thread[t2]: a.counter:0
create A
thread[t2]: a.counter:0