您的位置:首页 > 其它

线程的创建、终止、等待

2016-06-12 12:47 417 查看
线程的概念:

同一进程的多个线程共享同一地址空间,因此Text Segment、Data Segment都是共享的,如果定义一个函数,在各线程中都可以调用,如果定义一个全局变量,在各线程中都可以访问到,除此之外,各线程还共享以下进程资源和环境:
1. 文件描述符表
2. 每种信号的处理方式(SIG_IGN、SIG_DFL或者自定义的信号处理函数)
3. 当前工作目录
4. 用户id和组id

但有些资源是每个线程各有一份的:
1. 线程id
2. 上下文,包括各种寄存器的值、程序计数器和栈指针
3. 栈空间
4. errno变量
5. 信号屏蔽字
6. 调度优先级
注意:
编译时要加上-lpthread选项。

A. 创建线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
             void *(*start_routine) (void *), void *arg);

返回值:成功返回0,失败返回错误号。

以前学过的系统函数都是成功返回0,失败返回-1,而错误号保存在全局变量errno中,而pthread库的函数都是通过返回值返回错误号,由于pthread_create的错误码不保存在errno中,因此不能直接用perror(3)打印错误信息,可以先用strerror(3)把错误码转换成错误信息再打印。

pthread_create成功返回后,新创建的线程的id被填写到thread参数所指向的内存单元。我们知道进程id的类型是pid_t,每个进程的id在整个系统中是唯一的,调用getpid(2)可以获得当前进程的id,是一个正整数值。线程id的类型是thread_t,它只在当前进程中保证是唯一的。

B. 终止线程

注意:
1、任意一个线程函数中调用exit或_exit,整个进程的所有线程都终止。
2、从main函数中return,相当于调用exit,终止进程。

终止进程:
1、从线程函数return。
2、一个线程可以调用pthread_cancel终止同一进程中的另一个线程。
3、线程可以调用pthread_exit终止自己。

void pthread_exit(void *retval);
int pthread_cancel(pthread_t thread);
pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分 配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

c. 线程等待

int pthread_join(pthread_t thread, void **retval);
返回值:成功返回0,失败返回错误号

调用该函数的线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的,总结如下:

如果thread线程通过return返回,value_ptr所指向的单元里存放的是thread线程函数的返回值。

2. 如果thread线程被别的线程调用pthread_cancel异常终掉,value_ptr所指向的单元里存放的是常数PTHREAD_CANCELED。PTHREAD_CANCELED的值是-1

3. 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。如果对thread线程的终止状态不感兴趣,可以传NULL给value_ptr参数。

一般情况下,线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的状态为止。但是线程也可以被置为detach 状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态。不能对一个已经处于detach状态的线程调用pthread_join,这样的调用将返回EINVAL。对一个尚未detach的线程调用pthread_join或pthread_detach都可以把该线程置为detach状态,也 就是说,不能对同一线程调用两次pthread_join,或者如果已经对一个线程调用 了pthread_detach就不能再调用pthread_join了。

int pthread_detach(pthread_t thread);

返回值:成功返回0,失败返回错误号。  1 #include<stdio.h>
  2 #include<pthread.h>
  3 void* pthread_run1(void* arg)
  4 {
  5     printf("this is a thread1,pid:%d,tid:%ld\n",getpid(),pthread_self());
  6     return (void*)1;
  7 }
  8 
  9 void* pthread_run2(void* arg)
 10 {
 11     printf("this is a thread2,pid:%d,tid:%ld\n",getpid(),pthread_self());
 12     pthread_exit((void*)2);
 13 }
 14 
 15 void* pthread_run3(void* arg)
 16 {
 17     while(1)
 18     {
 19      printf("this is a thread3,pid:%d,tid:%ld\n",getpid(),pthread_self());
 20      sleep(1);
 21     }
 22      return NULL;
 23 }
 24 int main()
 25 {
 26     pthread_t tid;
 27     void* ret;
 28 
 29     pthread_create(&tid,NULL,pthread_run1,NULL);
 30     pthread_join(tid,&ret);
 31     printf("thread1,tid:%ld,return code:%d\n",tid,(int)ret);
 32 
 33     pthread_create(&tid,NULL,pthread_run2,NULL);
 34     pthread_join(tid,&ret);
 35     printf("thread2,tid:%ld,return code:%d\n",tid,(int)ret);
 36 
 37     pthread_create(&tid,NULL,pthread_run3,NULL);
 38     sleep(5);
 39     pthread_cancel(tid);
 40     pthread_join(tid,&ret);
 41     printf("thread3,tid:%ld,return code:%d\n",tid,(int)ret);
 42     return 0;
 43 }

结果:
[admin@localhost THREAD]$ vim thread.c
[admin@localhost THREAD]$ ./thread
this is a thread1,pid:2749,tid:-1216423056
thread1,tid:-1216423056,return code:1
this is a thread2,pid:2749,tid:-1216423056
thread2,tid:-1216423056,return code:2
this is a thread3,pid:2749,tid:-1216423056
this is a thread3,pid:2749,tid:-1216423056
this is a thread3,pid:2749,tid:-1216423056
this is a thread3,pid:2749,tid:-1216423056
this is a thread3,pid:2749,tid:-1216423056
thread3,tid:-1216423056,return code:-1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: