一个线程的生命周期起始于它被创建的那一刻,创建线程的接口:
#include
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
函数说明:
POSIX thread 简称 pthread
pthread_t 是 unsigned long int 类型的变量,用来表示线程的ID
函数参数:
thread(输出参数),由pthread_create在线程创建成功后返回的线程句柄,该句柄在后续操作线程的API中用于标志该新建的线程;
start_routine(输入参数),新建线程的入口函数;
arg(输入参数),传递给新线程入口函数的参数;
attr(输入参数),指定新建线程的属性,如线程栈大小等;如果值为NULL,表示使用系统默认属性。
函数返回值:
成功,返回0;
失败,返回相关错误码。
需要注意:
主线程,这是一个进程的初始线程,其入口函数为main函数。
新线程的运行时机,一个线程被创建之后有可能不会被马上执行,甚至,在创建它的线程结束后还没被执行;也有可能新线程在当前线程从pthread_create前就已经在运行,甚至,在pthread_create前从当前线程返回前新线程就已经执行完毕。
线程ID
在新线程被创建后,便有了一个在其所在进程内(线程依附于进程而存在)唯一的标识符,由pthread_t表示,称为线程ID。一个线程可以调用以下接口获取其ID:
include pthread_t pthread_self(void);
pthread_self直接返回调用线程的ID。
判断两个线程ID的大小是没有任何意义的,但有时可能需要判断两个给定的线程ID是否相等,使用以下接口:
include pthread_t pthread_self(void);
pthread_equal如果t1和t2所指定的线程ID相同,返回0;否则返回非0值。
从系统实现的角度观察线程的创建
创建一个新的线程,从系统实现的角度看,就是创建了一个新的可调度实体;同一个进程内的线程,共享绝大部分进程的资源,只有少部分信息是线程所特有的,如栈和线程特有数据等。下图(图片来源于《Linux/UNIX系统编程手册》)是假设一个进程内存在4个线程时,内存资源的分配情况:
可以看出,同一进程内的线程间除了栈是特有的,其他内存资源几乎都是共享的。共享意味着,多个线程可以同时修改某一内存区,且该修改对同一进程的所有线程都是可见的。
线程的终止
一个线程的终止分两种形式:被动终止和主动终止
被动终止有两种方式:
线程所在进程终止,任意线程执行exit函数,都会导致进程终止,从而导致依附于该进程的所有线程终止。
其他线程调用pthread_cancel请求取消该线程。
主动终止也有两种方式:
在线程的入口函数中执行return语句,main函数(主线程入口函数)执行return语句会导致进程终止,从而导致依附于该进程的所有线程终止。
线程调用pthread_exit函数,main函数(主线程入口函数)调用pthread_exit函数, 主线程终止,但如果该进程内还有其他线程存在,进程会继续存在,进程内其他线程继续运行。
线程终止函数:
include
void pthread_exit(void *retval);
线程调用pthread_exit函数会导致该调用线程终止,并且返回由retval指定的内容(如何获取返回值后面介绍)。
注意:retval不能指向该线程的栈空间,否则可能成为野指针!
管理线程的终止
线程的连接
一个线程的终止对于另外一个线程而言是一种异步的事件,有时我们想等待某个ID的线程终止了再去执行某些操作,pthread_join函数为我们提供了这种功能,该功能称为线程的连接:
include
int pthread_join(pthread_t thread, void **retval);
参数说明:
thread(输入参数),指定我们希望等待的线程
retval(输出参数),我们等待的线程终止时的返回值,就是在线程入口函数中return的值或者调用pthread_exit函数的参数
返回值:
成功时,返回0
错误时,返回正数错误码
当线程X连接线程Y时,如果线程Y仍在运行,则线程X会阻塞直到线程Y终止;如果线程Y在被连接之前已经终止了,那么线程X的连接调用会立即返回。
连接线程其实还有另外一层意义,一个线程终止后,如果没有人对它进行连接,那么该终止线程占用的资源,系统将无法回收,而该终止线程也会成为僵尸线程。因此,当我们去连接某个线程时,其实也是在告诉系统该终止线程的资源可以回收了。
注意:对于一个已经被连接过的线程再次执行连接操作, 将会导致无法预知的行为!**
线程的分离
有时我们并不在乎某个线程是不是已经终止了,我们只是希望如果某个线程终止了,系统能自动回收掉该终止线程所占用的资源。pthread_detach函数为我们提供了这个功能,该功能称为线程的分离:
#include
int pthread_detach(pthread_t thread);
参数说明:
thread(输入参数),指定希望执行分离操作的线程
返回值:
成功时,返回0
错误时,返回正数错误码
默认情况下,一个线程终止了,是需要在被连接后系统才能回收其占有的资源的。如果我们调用pthread_detach函数去分离某个线程,那么该线程终止后系统将自动回收其资源。
注意,一个线程如果已经被分离了,那么我们就无法再去连接它了
例子:
/** 文件名: thread_sample1.c
* 描述:演示线程基本操作*/#include#include#include#include
/*子线程1入口函数*/
void *thread_routine1(void *arg)
{
fprintf(stdout,"thread1: hello world!\n");
sleep(1);/*子线程1在此退出*/
returnNULL;
}/*子线程2入口函数*/
void *thread_routine2(void *arg)
{
fprintf(stdout,"thread2: I‘m running...\n");
pthread_t main_thread=(pthread_t)arg;/*分离自我,不能再被连接*/pthread_detach(pthread_self());/*判断主线程ID与子线程2ID是否相等*/
if (!pthread_equal(main_thread, pthread_self())) {
fprintf(stdout,"thread2: main thread id is not equal thread2\n");
}/*等待主线程终止*/pthread_join(main_thread, NULL);
fprintf(stdout,"thread2: main thread exit!\n");
fprintf(stdout,"thread2: exit!\n");
fprintf(stdout,"thread2: process exit!\n");/*子线程2在此终止,进程退出*/pthread_exit(NULL);
}int main(int argc, char *argv[])
{/*创建子线程1*/pthread_t t1;if (pthread_create(&t1, NULL, thread_routine1, NULL)!=0) {
fprintf(stderr,"create thread fail.\n");
exit(-1);
}/*等待子线程1终止*/pthread_join(t1, NULL);
fprintf(stdout,"main thread: thread1 terminated!\n\n");/*创建子线程2,并将主线程ID传递给子线程2*/pthread_t t2;if (pthread_create(&t2, NULL, thread_routine2, (void *)pthread_self())!=0) {
fprintf(stderr,"create thread fail.\n");
exit(-1);
}
fprintf(stdout,"main thread: sleeping...\n");
sleep(3);/*主线程使用pthread_exit函数终止,进程继续存在*/fprintf(stdout,"main thread: exit!\n");
pthread_exit(NULL);
fprintf(stdout,"main thread: never reach here!\n");return 0;
}