首页 > 代码库 > 线程变量

线程变量

 

__thread int my_var = 6;

#if T_DESC("global", 1)
pid_t gettid(void)
{  
    return syscall(SYS_gettid);  
} 
#endif

#if T_DESC("TU1", 1)
void thread_1(void)  
{  
    int i;  

    for(i=0; i<10; i++)  {
        printf("thread_1: pid=0x%x tid=0x%x self=0x%x\n", getpid(), gettid(), (int)pthread_self());
        my_var += 2;
        printf("thread_1: my_var=%d addr=0x%x \n", my_var, &my_var);
        sleep(1);  
    }  
    pthread_exit(0);  
}  
  
void thread_2(void)  
{  
    int i;  
    
    for(i=0; i<5; i++) {
        printf("thread_2: pid=0x%x tid=0x%x self=0x%x\n", getpid(), gettid(), (int)pthread_self());
        my_var += 3;
        printf("thread_2: my_var=%d addr=0x%x \n", my_var, &my_var);
        sleep(1);  
    }  
    pthread_exit(0);  
}  

thread_2: pid=0x741 tid=0x743 self=0x85254700
thread_2: my_var=9 addr=0x852546fc
thread_1: pid=0x741 tid=0x742 self=0x85a55700
thread_1: my_var=8 addr=0x85a556fc
thread_2: pid=0x741 tid=0x743 self=0x85254700
thread_2: my_var=12 addr=0x852546fc
thread_1: pid=0x741 tid=0x742 self=0x85a55700
thread_1: my_var=10 addr=0x85a556fc
thread_2: pid=0x741 tid=0x743 self=0x85254700
thread_2: my_var=15 addr=0x852546fc

 

 

线程变量