Linux多线程详解

来源:互联网 发布:windows qt embedded 编辑:程序博客网 时间:2024/06/03 16:55

线程也被称为轻权进程(lightweight process)

在传统的UNIX上,一个进程让另一个实体做某个事务是用fork派生子进程的方法处理的。派生子进程的代价比线程要昂贵得多,尤其是在父子进程之间、子 进程之间传递信息需要用IPC或其他方法通信。相对比,使用线程有许多优点,如创建线程要比创建进程快的多、一个进程中的线程共享相同的全局存储区等等。

Linux系统下的多线程遵循POSIX线程接口,称为pthread,在linux中,多线程需要使用的头文件为<pthread.h>,连接时需要使用库为libpthread.a

我们编写一个非常简单的例子:

[html] view plain copy
 print?
  1. //example_1.c  
  2. #include <stdio.h>  
  3. #include <pthread.h>   
  4. void * pthread_func_test(void * arg);   
  5. int main(){       
  6.     pthread_t pt1,pt2;       
  7.     pthread_create(&pt1,NULL,pthread_func_test,"This is the Thread_ONE");       
  8.     pthread_create(&pt2,NULL,pthread_func_test,"This is the Thread_TWO");       
  9.     sleep(1);              //不加上这句,看不到结果。  
  10. }  
  11.   
  12. void * pthread_func_test(void * arg){  
  13.     printf("%s /n ",arg);  
  14. }  

编译源文件:

[html] view plain copy
 print?
  1. gcc example_1.c -o example -lpthread  

编译环境:

平   台:FC6

版   本:2.6.18-1.2798.fc6

编译器:gcc 4.1.1 20070105 (Red Hat 4.1.1-51)

运行可执行文件:

[html] view plain copy
 print?
  1. ./example  

在终端上的输出结果:

[html] view plain copy
 print?
  1. This is the Thread_ONE  
  2. This is the Thread_TWO  

example_1这个例子中,一共产生了三个线程,第一个就是main所代表的主线程,另外两个就是pt1pt2分别代表的两个分支线程,这两个线程由pthread_create函数创建,执行的内容就是写在函数pthread_func_test里的东东。

上例涉及到的函数是:pthread_create()

函数原型如下:

[html] view plain copy
 print?
  1. int pthread_create(pthread_t *restrict thread,  
  2.            const pthread_attr_t *restrict attr,  
  3.            void *(*start_routine)(void*), void *restrict arg);  

参数点解:

1、每个线程都有自己的IDthread ID,可以简称tid,呵呵,是不是想起什么来了?。。。对,和pid有点象。其类型为pthread_tpthread_t在头文件/usr/include/bits/pthreadtypes.h中定义:

[html] view plain copy
 print?
  1. typedef unsigned long int pthread_t;  

         可以看成是线程的标志符。当成功创建一个新线程的时候,系统会为该线程分配一个tid,并将该值通过指针返回给调用它的程序。

2attr申明线程的属性。                        

     属性结构为pthread_attr_t,它在头文件/usr/include/pthread.h中定义。设为NULL,表示在这里我们只使用线程的默认属性就可以了。

3start_routine表示新创建的线程所要执行的例程。线程以调用该函数开始,直到由该 函数返回(return)终止这个线程,或者在start_routine所指向的函数中调用pthread_exit函数终止。 start_routine只有一个参数,该参数由随后的arg指针来指出。

4arg:也是一个指针,也就是start_routine指针所指向的函数的参数。

返回值:

  当pthread_create调用成功时,该调用返回0;否则,返回一个错误代码指出错误的类型。 

接下来,再看另两个重要的函数pthread_exitpthread_join

函数原型如下:

[html] view plain copy
 print?
  1. void pthread_exit( void * value_ptr );  

线程的终止可以是调用了pthread_exit或者该线程的例程结束。也就是说,一个线程可以隐式的退出,也可以显式的调用pthread_exit函数来退出。
pthread_exit函数唯一的参数value_ptr是函数的返回代码,只要pthread_join中的第二个参数value_ptr不是NULL,这个值将被传递给value_ptr

函数原型如下:

[html] view plain copy
 print?
  1. int pthread_join( pthread_t  thread, void * * value_ptr );  

函数pthread_join的作用是,等待一个线程终止。
调用pthread_join的线程将被挂起直到参数thread所代表的线程终止时为止。pthread_join是一个线程阻塞函数,调用它的函数将一直等到被等待的线程结束为止。

如果value_ptr不为NULL,那么线程thread的返回值存储在该指针指向的位置。该返回值可以是由pthread_exit给出的值,或者该线程被取消而返回PTHREAD_CANCELED
当一个非分离的线程终止后,该线程的内存资源(线程描述符和栈)并不会被释放,直到有线程对它使 用了pthread_join时才被释放。因此,必须对每个创建为非分离的线程调用一次pthread_join调用,以避免内存泄漏。否则当线程是可分 离的,调用pthread_exit,将终止该调用线程,并释放所有资源,没有线程等待它终止。

至多只能有一个线程等待给定的线程终止。如果已经有一个线程在等待thread线程终止了,那么再次调用pthread_join等待同一线程的线程将返回一个错误。

[html] view plain copy
 print?
  1. //example_2.c  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4.   
  5. void * pthread_func_test(void * arg);  
  6.   
  7. int main()  
  8. {  
  9.     pthread_t pt1,pt2;  
  10.     pthread_create(&pt1,NULL,pthread_func_test,"This is the Thread_ONE");  
  11.     pthread_create(&pt2,NULL,pthread_func_test,"This is the Thread_TWO");  
  12.     pthread_join(pt1,NULL);  
  13.     pthread_join(pt2,NULL);        //这行不写,会发生什么?或写成pthread_join(pt1,NULL);又会怎么样?  
  14. }  
  15.   
  16. void * pthread_func_test(void * arg)  
  17. {  
  18.     printf("%s  ",arg);  
  19.     pthread_exit(NULL);            //显式声明  
  20. }  

到此为止,我们就学习了三个重要的函数--createexitjoin,接下来讲解多线程编程中的线程互斥问题。

线程互斥

互斥操作,就是对某段代码或某个变量修改的时候只能有一个线程在执行这段代码,其他线程不能同时进入这段代码或同时修改该变量。这个代码或变量称为临界资源。

例如:有两个线程AB,临界资源为X,首先线程A进入,将X置为加锁状态,在A将锁打开之前的这段时间里,如果此 时恰巧线程B也欲获得X,但它发现X处于加锁状态,说明有其它线程正在执行互斥部分,于是,线程B将自身阻塞。。。线程A处理完毕,在退出前,将X解锁, 并将其它线程唤醒,于是线程B开始对X进行加锁操作了。通过这种方式,实现了两个不同线程的交替操作。

记住:一个互斥体永远不可能同时属于两个线程。或者处于锁定状态;或者空闲中,不属于任何一个线程。

代码如下:

[html] view plain copy
 print?
  1. //example_3.c  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4.   
  5. void * pthread_func_test(void * arg);  
  6.   
  7. pthread_mutex_t mu;  
  8.   
  9. int main()  
  10. {  
  11.     int i;  
  12.     pthread_t pt;  
  13.       
  14.     pthread_mutex_init(&mu,NULL);        //声明mu使用默认属性,此行可以不写  
  15.     pthread_create(&pt,NULL,pthread_func_test,NULL);  
  16.     for(i = 0; i < 3; i++)  
  17.     {  
  18.         pthread_mutex_lock(&mu);  
  19.         printf("主线程ID是:%lu  ",pthread_self());        //pthread_self函数作用:获得当前线程的id  
  20.         pthread_mutex_unlock(&mu);  
  21.         sleep(1);  
  22.     }      
  23. }  
  24.   
  25. void * pthread_func_test(void * arg)  
  26. {  
  27.     int j;  
  28.     for(j = 0; j < 3; j++)  
  29.     {  
  30.         pthread_mutex_lock(&mu);  
  31.         printf("新线程ID是:%lu  ",pthread_self());  
  32.         pthread_mutex_unlock(&mu);  
  33.         sleep(1);  
  34.     }  
  35. }  

终端输出结果:

[html] view plain copy
 print?
  1. 主线程ID是 : 3086493376  
  2. 新线程ID是 : 3086490512  
  3.   
  4. 主线程ID是 : 3086493376  
  5. 新线程ID是 : 3086490512  
  6.   
  7. 主线程ID是 : 3086493376  
  8. 新线程ID是 : 3086490512  

注:在你机器上运行的结果很可能与这里显示的不一样。

pthread_mutex_lock声明开始用互斥锁上锁,此后的代码直至调用 pthread_mutex_unlock为止,都处于加锁状态中,即同一时间只能被一个线程调用执行。当另一个线程执行到 pthread_mutex_lock处时,如果该锁此时被其它线程使用,那么该线程被阻塞,即程序将等待到其它线程释放此互斥锁。

上述例子中,涉及到了几个函数:pthread_mutex_init/pthread_mutex_lock/pthread_mutex_unlock/pthread_mutex_destroy/pthread_self

 函数原型:

[html] view plain copy
 print?
  1. int pthread_mutex_init(pthread_mutex_t *restrict mutex,  
  2.                        const pthread_mutexattr_t *restrict attr);  

函数作用:

初始化互斥体类型变量mutex,变量的属性由attr进行指定。attr设为NULL,即采用默认属性,这种方式与pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER的方式等价。

函数原型:  

[html] view plain copy
 print?
  1. int pthread_mutex_lock(pthread_mutex_t *mutex);  

函数作用:

用来锁住互斥体变量。如果参数mutex所指的互斥体已经被锁住了,那么发出调用的线程将被阻塞直到其他线程对mutex解锁为止。

函数原型:

[html] view plain copy
 print?
  1. int pthread_mutex_unlock(pthread_mutex_t *mutex);  

函数作用:

如果当前的线程拥有参数mutex所指定的互斥体,那么该函数调用将该互斥体解锁。

函数原型:

[html] view plain copy
 print?
  1. int pthread_mutex_destroy(pthread_mutex_t *mutex);  

函数作用:

用来释放互斥体所占用的资源。

函数原型:

[html] view plain copy
 print?
  1. pthread_t pthread_self(void);  

函数作用:获得线程自身的ID。前面我们已经提到过,pthread_t的类型为unsigned long int,所以在打印的时候要使用%lu方式,否则将产生奇怪的结果。

但是上面的代码并不完善,假设将循环次数修改得足够的长,打印后的结果可能并不是我们所希望看到的交替打印,可能象下面这样:

[html] view plain copy
 print?
  1. 主线程ID是 : 3086493376  
  2. 新线程ID是 : 3086490512  
  3.    
  4. 主线程ID是 : 3086493376  
  5. 新线程ID是 : 3086490512  
  6.   
  7. 新线程ID是 : 3086490512  
  8. 主线程ID是 : 3086493376  

这是什么原因呢?因为Linux是分时操作系统,采用的是时间片轮转的方式,主线程和新线程可能因为其它因素的干扰,获得了非顺序的时间片。如果想要严格的做到交替方式,可以略施小计,即加入一个标志。

完整程序如下:

[html] view plain copy
 print?
  1. //example_4.c  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4.   
  5. void * pthread_func_test(void * arg);  
  6.   
  7. pthread_mutex_t mu;  
  8. int flag = 0;  
  9.   
  10. int main()  
  11. {  
  12.     int i;  
  13.     pthread_t pt;  
  14.       
  15.     pthread_mutex_init(&mu,NULL);  
  16.     pthread_create(&pt,NULL,pthread_func_test,NULL);  
  17.     for(i = 0; i < 3; i++)  
  18.     {  
  19.         pthread_mutex_lock(&mu);  
  20.         if(flag == 0)  
  21.                 printf("主线程ID是:%lu  ",pthread_self());      
  22.         flag = 1;          
  23.         pthread_mutex_unlock(&mu);  
  24.         sleep(1);  
  25.     }  
  26.     pthread_join(pt, NULL);  
  27.     pthread_mutex_destroy(&mu);      
  28. }  
  29.   
  30. void * pthread_func_test(void * arg)  
  31. {  
  32.     int j;  
  33.     for(j = 0; j < 3; j++)  
  34.     {  
  35.         pthread_mutex_lock(&mu);  
  36.         if(flag == 1)  
  37.             printf("新线程ID是:%lu  ",pthread_self());  
  38.         flag == 0;  
  39.         pthread_mutex_unlock(&mu);  
  40.         sleep(1);  
  41.     }  
  42. }  

在使用互斥锁的过程中很有可能会出现死锁:即两个线程试图同时占用两个资源,并按不同的次序锁定相应的互斥锁,例如 两个线程都需要锁定互斥锁1和互斥锁2A线程先锁定互斥锁1B线程先锁定互斥锁2,这时就出现了死锁。此时我们可以使用函数 pthread_mutex_trylock,该函数企图锁住一个互斥体,但不阻塞。

函数原型:

[html] view plain copy
 print?
  1. int pthread_mutex_trylock(pthread_mutex_t *mutex);  

函数pthread_mutex_trylock()用来锁住参数mutex所指定的互斥体。如果参数mutex所 指的互斥体已经被上锁,该调用不会阻塞等待互斥体的解锁,而会返回一个错误代码。通过对返回代码的判断,程序员就可以针对死锁做出相应的处理。所以在对多 个互斥体编程中,尤其要注意这一点。

经过以上的讲解,我们就学习了linux下关于多线程方面对互斥体变量的操作。接下来将讲解有关线程同步方面的知识点。

线程同步

首先来看一下有关同步机制的概念。同步就是若干个线程等待某个事件的发生,当等待事件发生时,一起开始继续执行。可以这样简单理解同步,就是若干个线程各自对自己的数据进行处理,然后在某个点必须汇总一下数据,否则不能进行下一步的处理工作。

线程同步的函数调用有pthread_cond_initpthread_cond_broadcastpthread_cond_signalpthread_cond_waitpthread_cond_destroy

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_init(pthread_cond_t *restrict cond,  
  2.           const pthread_condattr_t *restrict attr);  

函数说明:

attr指定的属性初始化cond条件变量。如果attrNULL,效果等同于pthread_cond_t cond = PTHREAD_COND_INITIALIZER

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_broadcast(pthread_cond_t *cond);  
函数说明:
对所有等待cond这个条件变量的线程解除阻塞。

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_signal(pthread_cond_t *cond);  
函数说明:
仅仅解除等待cond这个条件变量的某一个线程的阻塞状态。如果有若干线程挂起等待该条件变量,该调用只唤起一个线程,被唤起的线程是哪一个是不确定的。

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_wait(pthread_cond_t *restrict cond,  
  2.        pthread_mutex_t *restrict mutex);  

函数说明:
该调用自动阻塞发出调用的当前线程,并等待由参数cond指 定的条件变量,而且为参数mutex指定的互斥体解锁。被阻塞线程直到有其他线程调用pthread_cond_signal或 pthread_cond_broadcast函数置相应的条件变量时,而且获得mutex互斥体才解除阻塞。等待状态下的线程不占用CPU时间。

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_timedwait(pthread_cond_t *restrict cond,  
  2.        pthread_mutex_t *restrict mutex,  
  3.        const struct timespec *restrict abstime);  

函数说明:
该函数自动阻塞当前线程等待参数cond指定的条件变量,并 为参数mutex指定的互斥体解锁。被阻塞的线程被唤起继续执行的条件是:有其他线程对条件变量cond调用pthread_cond_signal函 数;或有其他线程对条件变量cond调用pthread_cond_broadcast;或系统时间到达abstime参数指定的时间;除了前面三个条件 中要有一个被满足外,还要求该线程获得参数mutex指定的互斥体。

函数原型:

[html] view plain copy
 print?
  1. int pthread_cond_destroy(pthread_cond_t *cond);  

函数说明:
释放cond条件变量占用的资源。

看下面的示例:

[html] view plain copy
 print?
  1. //example_5.c  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4.   
  5. pthread_t pt1,pt2;  
  6. pthread_mutex_t mu;  
  7. pthread_cond_t cond;  
  8. int i = 1;  
  9.   
  10. void * decrease(void * arg)  
  11. {  
  12.     while(1)  
  13.     {  
  14.         pthread_mutex_lock(&mu);  
  15.         if(++i)  
  16.         {  
  17.             printf("%d  ",i);  
  18.             if(i != 1)    printf("Error  ");  
  19.             pthread_cond_broadcast(&cond);  
  20.             pthread_cond_wait(&cond,&mu);  
  21.         }  
  22.         sleep(1);  
  23.         pthread_mutex_unlock(&mu);  
  24.     }      
  25. }  
  26.   
  27. void * increase(void * arg)  
  28. {  
  29.     while(1)  
  30.     {  
  31.         pthread_mutex_lock(&mu);  
  32.         if(i--)  
  33.         {  
  34.             printf("%d  ",i);  
  35.             if(i != 0)    printf("Error  ");  
  36.             pthread_cond_broadcast(&cond);  
  37.             pthread_cond_wait(&cond,&mu);  
  38.         }  
  39.         sleep(1);  
  40.         pthread_mutex_unlock(&mu);  
  41.     }      
  42. }  
  43.   
  44.   
  45. int main()  
  46. {  
  47.     pthread_create(&pt2,NULL,increase,NULL);  
  48.     pthread_create(&pt1,NULL,decrease,NULL);  
  49.     pthread_join(pt1,NULL);  
  50.     pthread_join(pt2,NULL);  
  51. }  

 以上我们讲解过了Linux下利用pthread.h头文件的多线程编程知识。

==========================

原创作者:Frozen_socker(冰棍)   

  E_maildlskyfly@163.com