#include<stdio.h>
#include<pthread.h>
class Test{
public:
Test(){printf("c:%p\n",this);}
~Test(){printf("d:%p\n",this);}
};
void* func(void*arg){
static thread_local int a = 0;
static thread_local Test t;
int n = (int)arg;
for(int i = 0 ; i < 1000000 ; ++i)
++a;// printf("%d:%d\n",n,++a);
return (void*)a;
}
int main() {
pthread_t t1,t2;
pthread_create(&t1, nullptr, func,0);
pthread_create(&t2, nullptr, func,1);
int a,b;
pthread_join(t1, (void**)&a);
pthread_join(t2, (void**)&b);
printf("a + b:%d\n", a + b);
}
thread_local
最新推荐文章于 2025-05-25 13:14:48 发布
博客介绍了通过定义线程绑定变量,避免多线程竞争数据的方法,同时说明了可通过线程返回值返回最终结果,涉及C语言和C++编程及相关算法。
1242

被折叠的 条评论
为什么被折叠?



