MasterofProject

Linux multithreading practice (four) the specific data thread

label Multi threadthreadLinuxThread specific data
8364 people read comment(2) Collection report
Classification:

In single threaded programs, we often have to use global variables to achieve multiple functions for sharing data between. However, in a multi-threaded environment, due to the spatial data is shared, so global variables also for all threads. But sometimes it is necessary to provide an application design.Thread private global variables, only in a thread, but can be accessed across multiple functions. POSIX thread library to solve this problem by maintaining a certain data structure, the data is referred to as (Thread-specific-data or TSD).

Related functions are as follows:

Pthread_key_create int (*key pthread_key_t, void (*destr_function) (void *)) (*);
Pthread_key_delete int (key pthread_key_t);
  
Pthread_setspecific int (key pthread_key_t, void *pointer const);
Void * pthread_getspecific (key pthread_key_t);
  
Once_control pthread_once_t = PTHREAD_ONCE_INIT;
Pthread_once int (*once_control pthread_once_t, void (*init_routine) (void) ());

From the above chart shows: when calling pthread key create will produce a all threads are visible thread specific data (TSD) key (such as above all the threads will get the value of a pkey[1]. But the key point to the real data is different, although all is pkey[1], but they and not point to the same piece of memory, but point to the only belong to their actual data. Therefore, if 0 thread changes the pkey[1] pointing to the data, and is not able to image to the thread n;

Each thread specific data and thread key t binding in the thread by calling pthread Pthread_setspecific although only a pthread key t, but each thread specific data is an independent memory space, when the thread exits will execute the destructor function.

The use of pthread_once 1: / * * example, let key initialized only once
Note: the initialization of the key is put into the init_routine
**/
Key pthread_key_t;
Once_control pthread_once_t = PTHREAD_ONCE_INIT;
Struct Tsd typedef
{
TID pthread_t;
*str char;
} tsd_t;
  
/ / thread specific data destruction function,
/ / for the destruction of the actual data points to each thread
Destructor_function void (*value void)
{
Free (value);
Cout "destructor..." "endl" ";
}
  
/ / initialization function, the initialization of key into the function,
/ / inti_routine function can guarantee only once
Init_routine void ()
{
Pthread_key_create (&key, destructor_function);
Cout "init..." "endl" ";
}
  
*thread_routine void (*args void)
{
Pthread_once (&once_control, init_routine);
  
/ / set the thread specific data
*value tsd_t = (tsd_t *) malloc (sizeof (tsd_t));
Value->tid = pthread_self ();
Value->str = (char *) args;
Pthread_setspecific (key, value);
Printf ("setspecific%s,%p\n address:", (char *) args, value ();
  
/ / get the thread specific data
Value = (tsd_t *) pthread_getspecific (key);
Printf ("0x%x STR, =%s\n", (int value->tid) unsigned, value->str (tid:);
Sleep (2);
  
/ / get the thread specific data again
Value = (tsd_t *) pthread_getspecific (key);
Printf ("0x%x STR, =%s\n", (int value->tid) unsigned, value->str (tid:);
  
Pthread_exit (NULL);
}
  
Main int ()
{
TID1 tid2, pthread_t;
Pthread_create (&tid1, NULL, thread_routine, (void *) "thread1" ("");
Pthread_create (&tid2, NULL, thread_routine, (void *) "thread2" ("");
  
Pthread_join (TID1, NULL);
Pthread_join (tid2, NULL);
Pthread_key_delete (key);
  
Return 0;
}
Operation results are as follows:

Init...
Setspecific address:, 0x7fe7a00008c0 thread1
0xa8192700 STR, tid: = thread1
2 X,地址:0x7fe7980008c0
TID:0xa7991700,STR = 2
TID:0xa8192700,STR =线程1
TID:0xa7001700,STR = 2
析构函数…
析构函数…


主线程创建了两个线程然后加入等待他们退出;给每个线程的执行函数都是thread_routine,thread_routine中调用了pthread_once,此函数表示如果当第一个线程调用它时会执行once_routine,然后从once_routine返回即pthread_once返回,而接下去的其他线程调用它时将不再执行once_routine,此举是为了只调用pthread_key_create一次,即产生一个pthread_key_t 值。

在thread_routine函数中自定义了线程特定数据的类型,对于不同的线程来说TSD的内容不同,假设线程1在第一次打印完进入睡眠的时候,线程2也开始执行并调用pthread_setspecific绑定线程2的TSD和key_t,此时线程1调用pthread_getspecific返回key_t绑定的TSD指针,仍然是线程1的TSD指针,即虽然key_t只有一个,但每个线程都有自己的TSD。

特定数据;具有一百二十八项,通过主要价值实现,一个线程创建一个关键,其他线程也会创建,但是并不是指向的同一快内存,他们指向自己的数据,

这就是线程特定数据。

上述代码中,即使是睡眠(2),线程的数据并不会被线程的数据所影响,因为是线程私有的。

当线程退出的时候会销毁次,因为创建了两个线程。


其中TID是线程的ID,STR是传递给thread_routine的参数,可以看到有两个不同的PTR,且摧毁调用两次。

另外,关于Linux / Unix线程私有数据实现思想

请参考HTTP:/ / prog3。COM /深圳/博客/ caigen1988 /文章/细节/ 7901248,写的很好。



猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:163001次
    • 积分:二千九百九十五
    • 等级:
    • 排名:6758名第
    • 原创:155篇
    • 转载:7篇
    • 译文:0篇
    • 评论:58条
    联系方式
    博客专栏
    文章存档
easypdfformula.co