|  | 
 
| 9.2  Linux线程编程 
 9.2.1  线程基本编程
 
 这里要讲的线程相关操作都是用户空间中的线程的操作。在Linux中,一般pthread线程库是一套通用的线程库,是由POSIX提出的,因此具有很好的可移植性。
 
 (1)函数说明。
 创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是pthread_create()。在线程创建以后,就开始运行相关的线程函数,在该函数运行完之后,该线程也就退出了,这也是线程退出一种方法。另一种退出线程的方法是使用函数pthread_exit(),这是线程的主动行为。这里要注意的是,在使用线程函数时,不能随意使用exit()退出函数进行出错处理,由于exit()的作用是使调用进程终止,往往一个进程包含多个线程,因此,在使用exit()之后,该进程中的所有线程都终止了。因此,在线程中就可以使用pthread_exit()来代替进程中的exit()。
 
 由于一个进程中的多个线程是共享数据段的,因此通常在线程退出之后,退出线程所占用的资源并不会随着线程的终止而得到释放。正如进程之间可以用wait()系统调用来同步终止并释放资源一样,线程之间也有类似机制,那就是pthread_join()函数。pthread_join()可以用于将当前线程挂起来等待线程的结束。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源就被收回。
 
 前面已提到线程调用pthread_exit()函数主动终止自身线程。但是在很多线程应用中,经常会遇到在别的线程中要终止另一个线程的执行的问题。此时调用pthread_cancel()函数实现这种功能,但在被取消的线程的内部需要调用pthread_setcancel()函数和pthread_setcanceltype()函数设置自己的取消状态,例如被取消的线程接收到另一个线程的取消请求之后,是接受还是忽略这个请求;如果接受,是立刻进行终止操作还是等待某个函数的调用等。
 
 (2)函数格式。
 表9.1列出了pthread_create()函数的语法要点。
 表9.1                                            pthread_create()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_create ((pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg))
 
 |  | 函数传入值 
 | thread:线程标识符 
 |  | attr:线程属性设置(其具体设置参见9.2.3小节),通常取为NULL 
 |  | 
 | start_routine:线程函数的起始地址,是一个以指向void的指针作为参数和返回值的函数指针 
 |  | arg:传递给start_routine的参数 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.2列出了pthread_exit()函数的语法要点。
 表9.2                                              pthread_exit()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | void pthread_exit(void *retval) 
 |  | 函数传入值 
 | retval:线程结束时的返回值,可由其他函数如pthread_join()来获取 
 | 
 表9.3列出了pthread_join()函数的语法要点。
 表9.3                                              pthread_join()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_join ((pthread_t th, void **thread_return)) 
 |  | 函数传入值 
 | th:等待线程的标识符 
 |  | thread_return:用户定义的指针,用来存储被等待线程结束时的返回值(不为NULL时) 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.4列出了pthread_cancel()函数的语法要点。
 表9.4                                            pthread_cancel()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_cancel((pthread_t th) 
 |  | 函数传入值 
 | th:要取消的线程的标识符 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 (3)函数使用。
 以下实例中创建了3个线程,为了更好地描述线程之间的并行执行,让3个线程重用同一个执行函数。每个线程都有5次循环(可以看成5个小任务),每次循环之间会随机等待1~10s的时间,意义在于模拟每个任务的到达时间是随机的,并没有任何特定规律。
 
 /* thread.c */
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
 
 #define THREAD_NUMBER       3                 /*线程数*/
 #define REPEAT_NUMBER       5                 /*每个线程中的小任务数*/
 #define DELAY_TIME_LEVELS  10.0             /*小任务之间的最大时间间隔*/
 
 void *thrd_func(void *arg)
 { /* 线程函数例程 */
 int thrd_num = (int)arg;
 int delay_time = 0;
 int count = 0;
 
 printf("Thread %d is starting\n", thrd_num);
 for (count = 0; count < REPEAT_NUMBER; count++)
 {
 delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
 sleep(delay_time);
 printf("\tThread %d: job %d delay = %d\n",
 thrd_num, count, delay_time);
 }
 printf("Thread %d finished\n", thrd_num);
 pthread_exit(NULL);
 }
 
 int main(void)
 {
 pthread_t thread[THREAD_NUMBER];
 int no = 0, res;
 void * thrd_ret;
 
 srand(time(NULL));
 
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 /* 创建多线程 */
 res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);
 if (res != 0)
 {
 printf("Create thread %d failed\n", no);
 exit(res);
 }
 }
 
 printf("Create treads success\n Waiting for threads to finish...\n");
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 /* 等待线程结束 */
 res = pthread_join(thread[no], &thrd_ret);
 if (!res)
 {
 printf("Thread %d joined\n", no);
 }
 else
 {
 printf("Thread %d join failed\n", no);
 }
 }
 return 0;
 }
 
 以下是程序运行结果。可以看出每个线程的运行和结束是独立与并行的。
 
 $ ./thread
 Create treads success
 Waiting for threads to finish...
 Thread 0 is starting
 Thread 1 is starting
 Thread 2 is starting
 Thread 1: job 0 delay = 6
 Thread 2: job 0 delay = 6
 Thread 0: job 0 delay = 9
 Thread 1: job 1 delay = 6
 Thread 2: job 1 delay = 8
 Thread 0: job 1 delay = 8
 Thread 2: job 2 delay = 3
 Thread 0: job 2 delay = 3
 Thread 2: job 3 delay = 3
 Thread 2: job 4 delay = 1
 Thread 2 finished
 Thread 1: job 2 delay = 10
 Thread 1: job 3 delay = 4
 Thread 1: job 4 delay = 1
 Thread 1 finished
 Thread 0: job 3 delay = 9
 Thread 0: job 4 delay = 2
 Thread 0 finished
 Thread 0 joined
 Thread 1 joined
 Thread 2 joined
 
 9.2.2  线程之间的同步与互斥
 
 由于线程共享进程的资源和地址空间,因此在对这些资源进行操作时,必须考虑到线程间资源访问的同步与互斥问题。这里主要介绍POSIX中两种线程同步机制,分别为互斥锁和信号量。这两个同步机制可以互相通过调用对方来实现,但互斥锁更适合用于同时可用的资源是惟一的情况;信号量更适合用于同时可用的资源为多个的情况。
 
 1.互斥锁线程控制
 
 (1)函数说明。
 互斥锁是用一种简单的加锁方法来控制对共享资源的原子操作。这个互斥锁只有两种状态,也就是上锁和解锁,可以把互斥锁看作某种意义上的全局变量。在同一时刻只能有一个线程掌握某个互斥锁,拥有上锁状态的线程能够对共享资源进行操作。若其他线程希望上锁一个已经被上锁的互斥锁,则该线程就会挂起,直到上锁的线程释放掉互斥锁为止。可以说,这把互斥锁保证让每个线程对共享资源按顺序进行原子操作。
 
 互斥锁机制主要包括下面的基本函数。
 n     互斥锁初始化:pthread_mutex_init()
 n     互斥锁上锁:pthread_mutex_lock()
 n     互斥锁判断上锁:pthread_mutex_trylock()
 n     互斥锁接锁:pthread_mutex_unlock()
 n     消除互斥锁:pthread_mutex_destroy()
 
 其中,互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。这3种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时是否需要阻塞等待。快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止。递归互斥锁能够成功地返回,并且增加调用线程在互斥上加锁的次数,而检错互斥锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误信息。默认属性为快速互斥锁。
 
 (2)函数格式。
 表9.5列出了pthread_mutex_init()函数的语法要点。
 表9.5                                       pthread_mutex_init()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr) 
 |  | 函数传入值 
 | mutex:互斥锁 
 |  | 
 | Mutexattr 
 | PTHREAD_MUTEX_INITIALIZER:创建快速互斥锁 
 |  | PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:创建递归互斥锁 
 |  | PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:创建检错互斥锁 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.6列出了pthread_mutex_lock()等函数的语法要点。
 表9.6                                    pthread_mutex_lock()等函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_mutex_lock(pthread_mutex_t *mutex,) int pthread_mutex_trylock(pthread_mutex_t *mutex,)
 int pthread_mutex_unlock(pthread_mutex_t *mutex,)
 int pthread_mutex_destroy(pthread_mutex_t *mutex,)
 
 |  | 函数传入值 
 | mutex:互斥锁 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:-1 
 | 
 (3)使用实例。
 下面的实例是在9.2.1小节示例代码的基础上增加互斥锁功能,实现原本独立与无序的多个线程能够按顺序执行。
 
 /*thread_mutex.c*/
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
 
 #define THREAD_NUMBER        3            /* 线程数 */
 #define REPEAT_NUMBER        3            /* 每个线程的小任务数 */
 #define DELAY_TIME_LEVELS 10.0         /*小任务之间的最大时间间隔*/
 pthread_mutex_t mutex;
 
 void *thrd_func(void *arg)
 {
 int thrd_num = (int)arg;
 int delay_time = 0, count = 0;
 int res;
 /* 互斥锁上锁 */
 res = pthread_mutex_lock(&mutex);
 if (res)
 {
 printf("Thread %d lock failed\n", thrd_num);
 pthread_exit(NULL);
 }
 printf("Thread %d is starting\n", thrd_num);
 for (count = 0; count < REPEAT_NUMBER; count++)
 {
 delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
 sleep(delay_time);
 printf("\tThread %d: job %d delay = %d\n",
 thrd_num, count, delay_time);
 }
 printf("Thread %d finished\n", thrd_num);
 pthread_exit(NULL);
 }
 
 int main(void)
 {
 pthread_t thread[THREAD_NUMBER];
 int no = 0, res;
 void * thrd_ret;
 
 srand(time(NULL));
 /* 互斥锁初始化 */
 pthread_mutex_init(&mutex, NULL);
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);
 if (res != 0)
 {
 printf("Create thread %d failed\n", no);
 exit(res);
 }
 }
 printf("Create treads success\n Waiting for threads to finish...\n");
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 res = pthread_join(thread[no], &thrd_ret);
 if (!res)
 {
 printf("Thread %d joined\n", no);
 }
 else
 {
 printf("Thread %d join failed\n", no);
 }
 /* 互斥锁解锁 */
 pthread_mutex_unlock(&mutex);
 }
 pthread_mutex_destroy(&mutex);
 return 0;
 }
 
 该实例的运行结果如下所示。这里3个线程之间的运行顺序跟创建线程的顺序相同。
 
 
 $ ./thread_mutex
 Create treads success
 Waiting for threads to finish...
 Thread 0 is starting
 Thread 0: job 0 delay = 7
 Thread 0: job 1 delay = 7
 Thread 0: job 2 delay = 6
 Thread 0 finished
 Thread 0 joined
 Thread 1 is starting
 Thread 1: job 0 delay = 3
 Thread 1: job 1 delay = 5
 Thread 1: job 2 delay = 10
 Thread 1 finished
 Thread 1 joined
 Thread 2 is starting
 Thread 2: job 0 delay = 6
 Thread 2: job 1 delay = 10
 Thread 2: job 2 delay = 8
 Thread 2 finished
 Thread 2 joined
 
 2.信号量线程控制
 
 (1)信号量说明。
 在第8章中已经讲到,信号量也就是操作系统中所用到的PV原子操作,它广泛用于进程或线程间的同步与互斥。信号量本质上是一个非负的整数计数器,它被用来控制对公共资源的访问。这里先来简单复习一下PV原子操作的工作原理。
 
 PV原子操作是对整数计数器信号量sem的操作。一次P操作使sem减一,而一次V操作使sem加一。进程(或线程)根据信号量的值来判断是否对公共资源具有访问权限。当信号量sem的值大于等于零时,该进程(或线程)具有公共资源的访问权限;相反,当信号量sem的值小于零时,该进程(或线程)就将阻塞直到信号量sem的值大于等于0为止。
 
 PV原子操作主要用于进程或线程间的同步和互斥这两种典型情况。若用于互斥,几个进程(或线程)往往只设置一个信号量sem,它们的操作流程如图9.2所示。
 
 当信号量用于同步操作时,往往会设置多个信号量,并安排不同的初始值来实现它们之间的顺序执行,它们的操作流程如图9.3所示。
 
 
 
         图9.2  信号量互斥操作                                图9.3  信号量同步操作 
 (2)函数说明。
 Linux实现了POSIX的无名信号量,主要用于线程间的互斥与同步。这里主要介绍几个常见函数。
 
 n     sem_init()用于创建一个信号量,并初始化它的值。
 n     sem_wait()和sem_trywait()都相当于P操作,在信号量大于零时它们都能将信号量的值减一,两者的区别在于若信号量小于零时,sem_wait()将会阻塞进程,而sem_trywait()则会立即返回。
 n     sem_post()相当于V操作,它将信号量的值加一同时发出信号来唤醒等待的进程。
 n     sem_getvalue()用于得到信号量的值。
 n     sem_destroy()用于删除信号量。
 
 (3)函数格式。
 表9.7列出了sem_init()函数的语法要点。
 表9.7                                                  sem_init()函数语法要点
 
 | 所需头文件 
 | #include <semaphore.h> 
 |  | 函数原型 
 | int sem_init(sem_t *sem,int pshared,unsigned int value) 
 |  | 函数传入值 
 | sem:信号量指针 
 |  | pshared:决定信号量能否在几个进程间共享。由于目前Linux还没有实现进程间共享信号量,所以这个值只能够取0,就表示这个信号量是当前进程的局部信号量 
 |  | value:信号量初始化值 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:-1 
 | 
 表9.8列出了sem_wait()等函数的语法要点。
 表9.8                                               sem_wait()等函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int sem_wait(sem_t *sem) int sem_trywait(sem_t *sem)
 int sem_post(sem_t *sem)
 int sem_getvalue(sem_t *sem)
 int sem_destroy(sem_t *sem)
 
 |  | 函数传入值 
 | sem:信号量指针 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:-1 
 | 
 (4)使用实例。
 在前面已经通过互斥锁同步机制实现了多线程的顺序执行。下面的例子是用信号量同步机制实现3个线程之间的有序执行,只是执行顺序是跟创建线程的顺序相反。
 
 /*thread_sem.c*/
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
 #include <semaphore.h>
 
 #define THREAD_NUMBER        3                 /* 线程数 */
 #define REPEAT_NUMBER        3                 /* 每个线程中的小任务数 */
 #define DELAY_TIME_LEVELS   10.0             /*小任务之间的最大时间间隔*/
 sem_t sem[THREAD_NUMBER];
 
 void *thrd_func(void *arg)
 {
 int thrd_num = (int)arg;
 int delay_time = 0;
 int count = 0;
 /* 进行P操作 */
 sem_wait(&sem[thrd_num]);
 printf("Thread %d is starting\n", thrd_num);
 
 for (count = 0; count < REPEAT_NUMBER; count++)
 {
 delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
 sleep(delay_time);
 printf("\tThread %d: job %d delay = %d\n",
 thrd_num, count, delay_time);
 }
 
 printf("Thread %d finished\n", thrd_num);
 pthread_exit(NULL);
 }
 
 int main(void)
 {
 pthread_t thread[THREAD_NUMBER];
 int no = 0, res;
 void * thrd_ret;
 
 srand(time(NULL));
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 sem_init(&sem[no], 0, 0);
 res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);
 if (res != 0)
 {
 printf("Create thread %d failed\n", no);
 exit(res);
 }
 }
 
 printf("Create treads success\n Waiting for threads to finish...\n");
 /* 对最后创建的线程的信号量进行V操作 */
 sem_post(&sem[THREAD_NUMBER - 1]);
 for (no = THREAD_NUMBER - 1; no >= 0; no--)
 {
 res = pthread_join(thread[no], &thrd_ret);
 if (!res)
 {
 printf("Thread %d joined\n", no);
 }
 else
 {
 printf("Thread %d join failed\n", no);
 }
 /* 进行V操作 */
 sem_post(&sem[(no + THREAD_NUMBER - 1) % THREAD_NUMBER]);
 }
 
 for (no = 0; no < THREAD_NUMBER; no++)
 {
 /* 删除信号量 */
 sem_destroy(&sem[no]);
 }
 
 return 0;
 }
 
 该程序运行结果如下所示:
 
 $ ./thread_sem
 Create treads success
 Waiting for threads to finish...
 Thread 2 is starting
 Thread 2: job 0 delay = 9
 Thread 2: job 1 delay = 5
 Thread 2: job 2 delay = 10
 Thread 2 finished
 Thread 2 joined
 Thread 1 is starting
 Thread 1: job 0 delay = 7
 Thread 1: job 1 delay = 4
 Thread 1: job 2 delay = 4
 Thread 1 finished
 Thread 1 joined
 Thread 0 is starting
 Thread 0: job 0 delay = 10
 Thread 0: job 1 delay = 8
 Thread 0: job 2 delay = 9
 Thread 0 finished
 Thread 0 joined
 
 9.2.3  线程属性
 
 (1)函数说明。
 pthread_create()函数的第二个参数(pthread_attr_t *attr)表示线程的属性。在上一个实例中,将该值设为NULL,也就是采用默认属性,线程的多项属性都是可以更改的。这些属性主要包括绑定属性、分离属性、堆栈地址、堆栈大小以及优先级。其中系统默认的属性为非绑定、非分离、缺省1M的堆栈以及与父进程同样级别的优先级。下面首先对绑定属性和分离属性的基本概念进行讲解。
 
 n     绑定属性。
 前面已经提到,Linux中采用“一对一”的线程机制,也就是一个用户线程对应一个内核线程。绑定属性就是指一个用户线程固定地分配给一个内核线程,因为CPU时间片的调度是面向内核线程(也就是轻量级进程)的,因此具有绑定属性的线程可以保证在需要的时候总有一个内核线程与之对应。而与之对应的非绑定属性就是指用户线程和内核线程的关系不是始终固定的,而是由系统来控制分配的。
 n     分离属性。
 分离属性是用来决定一个线程以什么样的方式来终止自己。在非分离情况下,当一个线程结束时,它所占用的系统资源并没有被释放,也就是没有真正的终止。只有当pthread_join()函数返回时,创建的线程才能释放自己占用的系统资源。而在分离属性情况下,一个线程结束时立即释放它所占有的系统资源。这里要注意的一点是,如果设置一个线程的分离属性,而这个线程运行又非常快,那么它很可能在pthread_create()函数返回之前就终止了,它终止以后就可能将线程号和系统资源移交给其他的线程使用,这时调用pthread_create()的线程就得到了错误的线程号。
 
 这些属性的设置都是通过特定的函数来完成的,通常首先调用pthread_attr_init()函数进行初始化,之后再调用相应的属性设置函数,最后调用pthread_attr_destroy()函数对分配的属性结构指针进行清理和回收。设置绑定属性的函数为pthread_attr_setscope(),设置线程分离属性的函数为pthread_attr_setdetachstate(),设置线程优先级的相关函数为pthread_attr_getschedparam()(获取线程优先级)和pthread_attr_setschedparam()(设置线程优先级)。在设置完这些属性后,就可以调用pthread_create()函数来创建线程了。
 
 (2)函数格式。
 表9.9列出了pthread_attr_init()函数的语法要点。
 表9.9                                         pthread_attr_init()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_attr_init(pthread_attr_t *attr) 
 |  | 函数传入值 
 | attr:线程属性结构指针 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.10列出了pthread_attr_setscope()函数的语法要点。
 表9.10                                  pthread_attr_setscope()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_attr_setscope(pthread_attr_t *attr, int scope) 
 |  | 函数传入值 
 | attr:线程属性结构指针 
 |  | scope 
 | PTHREAD_SCOPE_SYSTEM:绑定 
 |  | PTHREAD_SCOPE_PROCESS:非绑定 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:-1 
 | 
 表9.11列出了pthread_attr_setdetachstate()函数的语法要点。
 表9.11                             pthread_attr_setdetachstate()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_attr_setscope(pthread_attr_t *attr, int detachstate) 
 |  | 函数传入值 
 | attr:线程属性 
 |  | detachstate 
 | PTHREAD_CREATE_DETACHED:分离 
 |  | PTHREAD _CREATE_JOINABLE:非分离 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.12列出了pthread_attr_getschedparam()函数的语法要点。
 表9.12                            pthread_attr_getschedparam()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_attr_getschedparam (pthread_attr_t *attr, struct sched_param *param) 
 |  | 函数传入值 
 | attr:线程属性结构指针 
 |  | param:线程优先级 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 表9.13列出了pthread_attr_setschedparam()函数的语法要点。
 表9.13                            pthread_attr_setschedparam()函数语法要点
 
 | 所需头文件 
 | #include <pthread.h> 
 |  | 函数原型 
 | int pthread_attr_setschedparam (pthread_attr_t *attr, struct sched_param *param) 
 |  | 函数传入值 
 | attr:线程属性结构指针 
 |  | param:线程优先级 
 |  | 函数返回值 
 | 成功:0 
 |  | 出错:返回错误码 
 | 
 (3)使用实例。
 下面的实例是在我们已经很熟悉的实例的基础上增加线程属性设置的功能。为了避免不必要的复杂性,这里就创建一个线程,这个线程具有绑定和分离属性,而且主线程通过一个finish_flag标志变量来获得线程结束的消息,而并不调用pthread_join()函数。
 
 /*thread_attr.c*/
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>
 
 #define REPEAT_NUMBER         3           /* 线程中的小任务数 */
 #define DELAY_TIME_LEVELS    10.0       /* 小任务之间的最大时间间隔 */
 int finish_flag = 0;
 
 void *thrd_func(void *arg)
 {
 int delay_time = 0;
 int count = 0;
 
 printf("Thread is starting\n");
 for (count = 0; count < REPEAT_NUMBER; count++)
 {
 delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
 sleep(delay_time);
 printf("\tThread : job %d delay = %d\n", count, delay_time);
 }
 
 printf("Thread finished\n");
 finish_flag = 1;
 pthread_exit(NULL);
 }
 
 int main(void)
 {
 pthread_t thread;
 pthread_attr_t attr;
 int no = 0, res;
 void * thrd_ret;
 
 srand(time(NULL));
 /* 初始化线程属性对象 */
 res = pthread_attr_init(&attr);
 if (res != 0)
 {
 printf("Create attribute failed\n");
 exit(res);
 }
 /* 设置线程绑定属性 */
 res = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
 /* 设置线程分离属性 */
 res += pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 if (res != 0)
 {
 printf("Setting attribute failed\n");
 exit(res);
 }
 
 res = pthread_create(&thread, &attr, thrd_func, NULL);
 if (res != 0)
 {
 printf("Create thread failed\n");
 exit(res);
 }
 /* 释放线程属性对象 */
 pthread_attr_destroy(&attr);
 printf("Create tread success\n");
 
 while(!finish_flag)
 {
 printf("Waiting for thread to finish...\n");
 sleep(2);
 }
 return 0;
 }
 
 接下来可以在线程运行前后使用“free”命令查看内存的使用情况。以下是运行结果:
 
 $ ./thread_attr
 Create tread success
 Waiting for thread to finish...
 Thread is starting
 Waiting for thread to finish...
 Thread : job 0 delay = 3
 Waiting for thread to finish...
 Thread : job 1 delay = 2
 Waiting for thread to finish...
 Waiting for thread to finish...
 Waiting for thread to finish...
 Waiting for thread to finish...
 Thread : job 2 delay = 9
 Thread finished
 
 /* 程序运行之前 */
 $ free
 total       used       free     shared    buffers     cached
 Mem:        255556     191940      63616         10       5864      61360
 -/+ buffers/cache:     124716     130840
 Swap:       377488      18352     359136
 
 /* 程序运行之中 */
 $ free
 total       used       free     shared    buffers     cached
 Mem:        255556     191948      63608         10       5888      61336
 -/+ buffers/cache:     124724     130832
 Swap:       377488      18352     359136
 
 /* 程序运行之后 */
 $ free
 total       used       free     shared    buffers     cached
 Mem:        255556     191940      63616         10       5904      61320
 -/+ buffers/cache:     124716     130840
 Swap:       377488      18352     359136
 
 可以看到,线程在运行结束后就收回了系统资源,并释放内存。
 | 
 |