pthread库中操作线程专有数据的函数:pthread_key_create,pthread_setspecific,pthread_gtespecific,pthread_key_delete

来源:互联网 发布:怎么用excel筛选数据 编辑:程序博客网 时间:2024/04/29 21:06

pthread库windows版下载地址:

http://sourceware.org/pthreads-win32/

pthread库中操作线程专有数据帮助文档地址:

http://sourceware.org/pthreads-win32/manual/index.html

为线程特定数据创建键

单线程 C 程序有两类基本数据:局部数据和全局数据。对于多线程 C 程序,添加了第三类数据:线程特定数据。线程特定数据与全局数据非常相似,区别在于前者为线程专有。

线程特定数据基于每线程进行维护。TSD(特定于线程的数据)是定义和引用线程专用数据的唯一方法。每个线程特定数据项都与一个作用于进程内所有线程的键关联。通过使用key,线程可以访问基于每线程进行维护的指针 (void *)。

pthread_key_create 语法

int pthread_key_create(pthread_key_t *key,        void (*destructor) (void *));
#include <pthread.h>        pthread_key_t key;    int ret;        /* key create without destructor */    ret = pthread_key_create(&key, NULL);        /* key create with destructor */    ret = pthread_key_create(&key, destructor); 

可以使用 pthread_key_create(3C) 分配用于标识进程中线程特定数据的。键对进程中的所有线程来说是全局的。创建线程特定数据时,所有线程最初都具有与该键关联的NULL 值。

使用各个键之前,会针对其调用一次 pthread_key_create()。不存在对键(为进程中所有的线程所共享)的隐含同步。

创建键之后,每个线程都会将一个值绑定到该键。这些值特定于线程并且针对每个线程单独维护。如果创建该键时指定了 destructor 函数,则该线程终止时,系统会解除针对每线程的绑定。

pthread_key_create() 成功返回时,会将已分配的键存储在 key 指向的位置中。调用方必须确保对该键的存储和访问进行正确的同步。

使用可选的析构函数 destructor 可以释放过时的存储。如果某个键具有非 NULL destructor 函数,而线程具有一个与该键关联的非NULL 值,则该线程退出时,系统将使用当前的相关值调用destructor 函数。destructor 函数的调用顺序不确定。

pthread_key_create 返回值

pthread_key_create() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下任一情况,pthread_key_create() 将失败并返回相应的值。

EAGAIN

描述:

key 名称空间已经用完。

ENOMEM

描述:

此进程中虚拟内存不足,无法创建新键。

删除线程特定数据键

使用 pthread_key_delete(3C) 可以销毁现有线程特定数据键。由于键已经无效,因此将释放与该键关联的所有内存。引用无效键将返回错误。Solaris 线程中没有类似的函数。

pthread_key_delete 语法

int pthread_key_delete(pthread_key_t key);
#include <pthread.h>        pthread_key_t key;    int ret;        /* key previously created */    ret = pthread_key_delete(key); 

如果已删除,则使用调用 pthread_setspecific()pthread_getspecific() 引用该键时,生成的结果将是不确定的。

程序员在调用删除函数之前必须释放所有线程特定资源。删除函数不会调用任何析构函数。反复调用 pthread_key_create()pthread_key_delete() 可能会产生问题。如果pthread_key_delete() 将键标记为无效,而之后key 的值不再被重用,那么反复调用它们就会出现问题。对于每个所需的键,应当只调用pthread_key_create() 一次。

pthread_key_delete 返回值

pthread_key_delete() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下情况,pthread_key_delete() 将失败并返回相应的值。

EINVAL

描述:

key 的值无效。

设置线程特定数据

使用 pthread_setspecific(3C) 可以为指定线程特定数据键设置线程特定绑定。

pthread_setspecific 语法

int pthread_setspecific(pthread_key_t key, const void *value);
#include <pthread.h>        pthread_key_t key;    void *value;    int ret;        /* key previously created */    ret = pthread_setspecific(key, value); 

pthread_setspecific 返回值

pthread_setspecific() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下任一情况,pthread_setspecific() 将失败并返回相应的值。

ENOMEM

描述:

虚拟内存不足。

EINVAL

描述:

key 无效。


注 –

设置新绑定时,pthread_setspecific() 不会释放其存储空间。必须释放现有绑定,否则会出现内存泄漏。


获取线程特定数据

请使用 pthread_getspecific(3C) 获取调用线程的绑定,并将该绑定存储在 value 指向的位置中。

pthread_getspecific 语法

void *pthread_getspecific(pthread_key_t key);
#include <pthread.h>        pthread_key_t key;    void *value;        /* key previously created */    value = pthread_getspecific(key); 

pthread_getspecific 返回值

pthread_getspecific 不返回任何错误。

全局和专用线程特定数据的示例

示例 2–2 显示的代码是从多线程程序中摘录出来的。这段代码可以由任意数量的线程执行,但该代码引用了两个全局变量:errnomywindow。这些全局值实际上应当是对每个线程专用项的引用。


示例 2–2 线程特定数据-全局但专用

body() {        ...            while (write(fd, buffer, size) == -1) {            if (errno != EINTR) {                fprintf(mywindow, "%s\n", strerror(errno));                exit(1);            }        }            ...        }

errno 引用应该从线程所调用的例程获取系统错误,而从其他线程所调用的例程获取系统错误。因此,线程不同,引用errno 所指向的存储位置也不同。

mywindow 变量指向一个 stdio (标准 IO)流,作为线程专属的流窗口。因此,与 errno 一样,线程不同,引用mywindow 所指向的存储位置也不同。最终,这个引用指向不同的流窗口。唯一的区别在于系统负责处理errno,而程序员必须处理对mywindow 的引用。

下一个示例说明对 mywindow 的引用如何工作。预处理程序会将对 mywindow 的引用转换为对 _mywindow() 过程的调用。

此例程随后调用 pthread_getspecific()pthread_getspecific() 接收mywindow_key 全局变量作为输入参数,以输出参数win 返回该线程的窗口。

示例 2–3 将全局引用转化为专用引用


 

thread_key_t mywin_key;        FILE *_mywindow(void) {        FILE *win;            win = pthread_getspecific(mywin_key);        return(win);    }        #define mywindow _mywindow()        void routine_uses_win( FILE *win) {        ...    }        void thread_start(...) {        ...        make_mywin();        ...        routine_uses_win( mywindow )        ...    }

mywin_key 变量标识一类变量,对于该类变量,每个线程都有其各自的专用副本。这些变量是线程特定数据。每个线程都调用 make_mywin() 以初始化其时限并安排其mywindow 实例以引用线程特定数据。

 

调用此例程之后,此线程可以安全地引用 mywindow,调用 _mywindow() 之后,此线程将获得对其专用时限的引用。引用mywindow 类似于直接引用线程专用数据。

 

示例 2–4 说明如何设置引用。

 


示例 2–4 初始化线程特定数据


 

void make_mywindow(void) {        FILE **win;        static pthread_once_t mykeycreated = PTHREAD_ONCE_INIT;            pthread_once(&mykeycreated, mykeycreate);            win = malloc(sizeof(*win));        create_window(win, ...);            pthread_setspecific(mywindow_key, win);    }        void mykeycreate(void) {        pthread_key_create(&mywindow_key, free_key);    }        void free_key(void *win) {        free(win);    }

首先,得到一个唯一的键值,mywin_key。此键用于标识线程特定数据类。第一个调用 make_mywin() 的线程最终会调用pthread_key_create(),该函数将唯一的key 赋给其第一个参数。第二个参数是destructor 函数,用于在线程终止后将该线程的特定于该线程的数据项实例解除分配。

 

接下来为调用方的线程特定数据项的实例分配存储空间。获取已分配的存储空间,调用 create_window(),以便为该线程设置时限。win 指向为该时限分配的存储空间。最后,调用pthread_setspecific(),将win 与该键关联。

 

以后,每当线程调用 pthread_getspecific() 以传递全局 key,线程都会获得它在前一次调用pthread_setspecific() 时设置的与该键关联的值)。

 

线程终止时,会调用在 pthread_key_create() 中设置的 destructor 函数。每个destructor 函数仅在终止线程通过调用pthread_setspecific()key 赋值之后才会被调用。

 

举例

下面说一下线程存储的具体用法。
1) 创建一个类型为 pthread_key_t 类型的变量。

2)调用 pthread_key_create() 来创建该变量。该函数有两个参数,第一个参数就是上面声明的 pthread_key_t 变量,

第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。该函数指针可以设成 NULL ,这样系统将调用默认的清理函数。

3)当线程中需要存储特殊值的时候,可以调用 pthread_setspcific() 。该函数有两个参数,第一个为前面声明的 pthread_key_t 变量,

第二个为 void* 变量,这样你可以存储任何类型的值。

4) 如果需要取出所存储的值,调用 pthread_getspecific() 。该函数的参数为前面提到的 pthread_key_t 变量,该函数返回

void * 类型的值。


下面是前面提到的函数的原型:

int pthread_setspecific(pthread_key_t key, const void *value);

void *pthread_getspecific(pthread_key_t key);

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

下面是一个如何使用线程存储的例子:

#include <malloc.h>

#include <pthread.h>

#include <stdio.h>

/* The key used to associate a log file pointer with each thread. */

static pthread_key_t thread_log_key;

/* Write MESSAGE to the log file for the current thread. */

void write_to_thread_log (const char* message)

{

FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);

fprintf (thread_log, "%s\n", message);

}

/* Close the log file pointer THREAD_LOG. */

void close_thread_log (void* thread_log)

{

fclose ((FILE*) thread_log);

}

void* thread_function (void* args)

{

char thread_log_filename[20];//="c:\\log_file.txt";

FILE* thread_log;

/* Generate the filename for this thread’s log file. */

sprintf (thread_log_filename, "thread%d.log",  pthread_self ().x);

/* Open the log file. */
//thread_log = fopen (thread_log_filename,"w+");
thread_log = fopen ("c:\\log_file.txt","w+");

/* Store the file pointer in thread-specific data under thread_log_key. */

pthread_setspecific (thread_log_key, thread_log);

write_to_thread_log ("Thread starting.");

/* Do work here... */

return NULL;

}

int main ()

{

int i;

pthread_t threads[5];

/* Create a key to associate thread log file pointers in

thread-specific data. Use close_thread_log to clean up the file

pointers. */

pthread_key_create (&thread_log_key, close_thread_log);

/* Create threads to do the work. */

for (i = 0; i < 5; ++i)

pthread_create (&(threads[i]), NULL, thread_function, NULL);

/* Wait for all threads to finish. */

for (i = 0; i < 5; ++i)

pthread_join (threads[i], NULL);

return 0;





最后说一下线程的本质。

其实在Linux 中,新建的线程并不是在原先的进程中,而是系统通过一个系统调用clone() 。
该系统copy 了一个和原先进程完全一样的进程,并在这个进程中执行线程函数。不过这个copy 过程和fork 不一样。
copy 后的进程和原先的进程共享了所有的变量,运行环境。这样,原先进程中的变量变动在copy 后的进程中便能体现出来
原创粉丝点击