多线程编程--- __thread关键字

来源:互联网 发布:vb picture 图形放大 编辑:程序博客网 时间:2024/06/06 00:31

__thread是GCC内置的线程局部存储设施,存取效率可以和全局变量相比。__thread变量每一个线程有一份独立实体,各个线程的值互不干扰。可以用来修饰那些带有全局性且值可能变,但是又不值得用全局变量保护的变量。

       __thread使用规则:只能修饰POD类型(类似整型指针的标量,不带自定义的构造、拷贝、赋值、析构的类型,二进制内容可以任意复制memset,memcpy,且内容可以复原),不能修饰class类型,因为无法自动调用构造函数和析构函数,可以用于修饰全局变量,函数内的静态变量,不能修饰函数的局部变量或者class的普通成员变量,且__thread变量值只能初始化为编译器常量(值在编译器就可以确定const int i=5,运行期常量是运行初始化后不再改变const int i=rand()).

 

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<pthread.h>  
  3. #include<unistd.h>  
  4. using namespace std;  
  5. const int i=5;  
  6. __thread int var=i;//两种方式效果一样  
  7. //__thread int var=5;//  
  8. void* worker1(void* arg);  
  9. void* worker2(void* arg);  
  10. int main(){  
  11.     pthread_t pid1,pid2;  
  12.     //__thread int temp=5;  
  13.     static __thread  int temp=10;//修饰函数内的static变量  
  14.     pthread_create(&pid1,NULL,worker1,NULL);  
  15.     pthread_create(&pid2,NULL,worker2,NULL);  
  16.     pthread_join(pid1,NULL);  
  17.     pthread_join(pid2,NULL);  
  18.     cout<<temp<<endl;//输出10  
  19.     return 0;  
  20. }  
  21. void* worker1(void* arg){  
  22.     cout<<++var<<endl;//输出 6  
  23. }  
  24. void* worker2(void* arg){  
  25.     sleep(1);//等待线程1改变var值,验证是否影响线程2  
  26.     cout<<++var<<endl;//输出6  
  27. }  
程序输出:

6

6         //可见__thread值线程间互不干扰

10



转自:http://blog.csdn.net/liuxuejiang158blog/article/details/14100897



====================================================================

__thread 和 __typeof__关键字  

 

__thread:在多线程变成中,使用于global变量,使每个线程都私有一份
static __thread int count;
void *function1(void *argc)
{
printf("porgran pid:%u, the function1 pthread id is %lu, count:%d\n",getpid(), pthread_self(), count);
count = 10;
printf("porgran pid:%u, last the function1 pthread id is %lu, count:%d\n",getpid(), pthread_self(), count);

return 0;
}
void *function2(void *argc)
{
printf("porgran pid:%u, the function2 pthread id is %lu, count:%d\n", getpid(), pthread_self(), count);
sleep(2);
count = 100;
printf("porgran pid:%u, last the function2 pthread id is %lu, count:%d\n", getpid(), pthread_self(), count);

return 0;
}

int main()
{
pthread_t  thread_id[2];
int ret;
pthread_t mian_thread_id;
mian_thread_id = pthread_self();
count = 2;
printf("porgran pid:%u, mian_thread_id:%lu, count:%d\n", getpid(), mian_thread_id, count);

ret = pthread_create(thread_id, NULL, function1, NULL);
assert(ret == 0);

ret = pthread_create(thread_id + 1, NULL, function2, NULL);
assert(ret == 0);

ret = pthread_join(thread_id[0], NULL);
assert(ret == 0);
ret = pthread_join(thread_id[1], NULL);
assert(ret == 0);

count = 1000;
printf("porgran pid:%u, last mian_thread_id:%lu, count:%d\n", getpid(), mian_thread_id, count);
return 0;
}

__typeof__(var) 是gcc对C语言的一个扩展保留字,用于声明变量类型,var可以是数据类型(int, char*..),也可以是变量表达式。

define DEFINE_MY_TYPE(type, name) __thread __typeof__(type) my_var_##name

DEFINE_MY_TYPE(int, one); //It   is   equivalent   to  '__thread int  my_var_'; which is a thread variable.
int main()
{
__typeof__(int *) x; //It   is   equivalent   to  'int  *x';

__typeof__(int) a;//It   is   equivalent   to  'int  a';

__typeof__(*x)  y;//It   is   equivalent   to  'int y';

__typeof__(&a) b;//It   is   equivalent   to  'int  b';

__typeof__(__typeof__(int *)[4])   z; //It   is   equivalent   to  'int  *z[4]';
y = *x;
b = &a;
z[0] = x;
z[1] = &a;
return 0;
}