Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5085881
  • 博文数量: 553
  • 博客积分: 13864
  • 博客等级: 上将
  • 技术积分: 11041
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-28 21:25
个人简介

个人Blog: hhktony.com

文章分类

全部博文(553)

文章存档

2015年(1)

2014年(2)

2013年(12)

2012年(384)

2011年(154)

分类: LINUX

2012-04-08 19:30:06

在Posix线程规范中还有几个辅助函数难以归类,暂且称其为杂项函数,主要包括pthread_self()、pthread_equal()和 pthread_once()三个,另外还有一个LinuxThreads非可移植性扩展函数 pthread_kill_other_threads_np()。本文就介绍这几个函数的定义和使用。

pthread_t pthread_self(void)

本函数返回本线程的标识符。

在LinuxThreads中,每个线程都用一个pthread_descr结构来描述,其中包含了线程状态、线程ID等所有需要的数据结构,此函数的实现就是在线程栈帧中找到本线程的pthread_descr结构,然后返回其中的p_tid项。

pthread_t类型在LinuxThreads中定义为无符号长整型。

int pthread_equal(pthread_t thread1, pthread_t thread2)

判断两个线程描述符是否指向同一线程。在LinuxThreads中,线程ID相同的线程必然是同一个线程,因此,这个函数的实现仅仅判断thread1和thread2是否相等。

int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))

本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. pthread_once_t once = PTHREAD_ONCE_INIT;
  4. void once_run(void)
  5. {
  6.     printf("once_run in thread %d\n", pthread_self());
  7. }

  8. void *child1(void *arg)
  9. {
  10.     int tid = pthread_self();
  11.     printf("thread1 %d enter\n", tid);
  12.     pthread_once(&once, once_run);
  13.     printf("thread1 %d returns\n", tid);
  14. }

  15. void *child2(void *arg)
  16. {
  17.     int tid = pthread_self();
  18.     printf("thread2 %d enter\n", tid);
  19.     pthread_once(&once, once_run);
  20.     printf("thread2 %d returns\n", tid);
  21. }

  22. int main(int argc, char *argv[])
  23. {
  24.     int tid1, tid2;
  25.     printf("hello\n");
  26.     pthread_create(&tid1, NULL, child1, NULL);
  27.     pthread_create(&tid2, NULL, child2, NULL);
  28.     sleep(10);
  29.     printf("main thread exit\n");
  30.     return 0;
  31. }
运行结果:
---> ./a.out
hello
thread1 -1216922768 enter
thread2 -1225315472 enter
once_run in thread -1216922768
thread1 -1216922768 returns
thread2 -1225315472 returns
main thread exit

once_run()函数仅执行一次,且究竟在哪个线程中执行是不定的,尽管pthread_once(&once,once_run)出现在两个线程中。

LinuxThreads使用互斥锁和条件变量保证由pthread_once()指定的函数执行且仅执行一次,而once_control则表征是否执 行过。如果once_control的初值不是PTHREAD_ONCE_INIT(LinuxThreads定义为0),pthread_once() 的行为就会不正常。在LinuxThreads中,实际"一次性函数"的执行状态有三种:NEVER(0)、IN_PROGRESS(1)、 DONE(2),如果once初值设为1,则由于所有pthread_once()都必须等待其中一个激发"已执行一次"信号,因此所有 pthread_once()都会陷入永久的等待中;如果设为2,则表示该函数已执行过一次,从而所有pthread_once()都会立即返回0。

void pthread_kill_other_threads_np(void)

这个函数是LinuxThreads针对本身无法实现的POSIX约定而做的扩展。POSIX要求当进程的某一个线程执行exec*系统调用在进程空间中 加载另一个程序时,当前进程的所有线程都应终止。由于LinuxThreads的局限性,该机制无法在exec中实现,因此要求线程执行exec前手工终 止其他所有线程。pthread_kill_other_threads_np()的作用就是这个。

需要注意的是,pthread_kill_other_threads_np()并没有通过pthread_cancel()来终止线程,而是直接向管理 线程发"进程退出"信号,使所有其他线程都结束运行,而不经过Cancel动作,当然也不会执行退出回调函数。尽管LinuxThreads的实验结果与 文档说明相同,但代码实现中却是用的__pthread_sig_cancel信号来kill线程,应该效果与执行pthread_cancel()是一 样的,其中原因目前还不清楚。

阅读(1729) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

重返人生2012-04-10 22:39:32

有时候就是很难搞清楚的……