__thread int i; extern __thread struct state s; static __thread char *p;这个关键字__thread可以单独使用,也可以和extern或者static配合使用,不能与其他的存储类说明符使用。当使用extern或者static,__thread必须在这些存储关键字后面使用。
/*File : thread.c *Auth : sjin *Date : 20141023 *Mail : 413977243@qq.com */ #include <stdio.h> #include <pthread.h> #define MAX_THREADS 2 static __thread int i = 1; void *thr_func(void *arg) { printf("pself = %d,i = %d\n",pthread_self(),i); i = ( int)pthread_self(); printf("pself = %d,i = %d\n",pthread_self(),i); } int main() { int j = 0; pthread_t thread_id[MAX_THREADS]; for(j = 0; j < MAX_THREADS;j++){ pthread_create(&thread_id[j],0,thr_func,NULL); } for(j = 0; j < MAX_THREADS;j++){ pthread_join(thread_id[j],NULL); } return 0; }运行结果:
pself = -1218581696,i = 1 pself = -1218581696,i = -1218581696 pself = -1226974400,i = 1 pself = -1226974400,i = -1226974400
原文地址:http://blog.csdn.net/sjin_1314/article/details/40391245