linux多线程学习(二)—线程的创建和退出

 

    在上一篇文章中对线程进行了简单的概述,它在系统中和编程的应用中,扮演的角色是不言而喻的。学习它、掌握它、吃透它是作为一个程序员的必须作为。在接下来的讲述中,所有线程的操作都是用户级的操作。在LINUX中,一般pthread线程库是一套通用的线程库,是由POSIX提出的,因此他的移植性是非常好的。

      创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是pthread_create。在线程创建之后,就开始运行相关的线程函数。在该函数运行结束,线程也会随着退出。这是其中退出线程的一种方法,另外一种退出线程的方法就是调用pthread_exit()函数接口,这是结束函数的主动行为。在这里要注意的是,在使用线程函数时,不要轻易调用exit()函数,因为这样会使整个进程退出,往往一个进程包含着多个线程,所以调用了exit()之后,所有该进程中的线程都会被结束掉。因此,在线程中,利用pthread_exit来替代进程中的exit。

      由于一个进程中的数据段是共享的,因此通常在线程退出之后,退出线程所占的资源并不会随着线程的结束而得到释放。正如进程之间可以调用wait()函数来同步中指并释放资源一样,线程之间也有类似的机制,那就是pthread_join函数.pthread_join可以将当前线程挂起,等待线程的结束,这个函数是一个阻塞函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待函数的资源就会被释放。

1、函数语法简述。

pthread_create

头文件:       pthread.h

函数原型:    int pthread_create (pthread_t* thread, pthread_attr_t* attr,

                                                  void* (start_routine)(void*), void* arg);

函数传入值: thread: 线程标识符

                   attr: 线程属性设置

                   start_routine:线程函数入口

                   arg:传递给线程函数的参数

返回值:       0: 成功

                   -1: 失败 

pthread_exit

头文件:      pthread.h

函数原型:   void pthread_exit (void*  retval);

函数传入值:retval:pthread_exit()调用者线程的返回值,可又其他函数如pthread_join来检索获取。

phread_join

头文件:      pthread.h

函数原型:   int pthread_join (pthread_t* thread, void** thread_return);

函数传入值:thread:等待线程的标识符。

                  thread_return:用户定义的指针,用来存储被等待线程的返回值(不为NULL值);

函数返回值:成功: 0

                  失败:-1

2、函数举例实现。

 

  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4. #include <errno.h>  
  5.   
  6. static void pthread_func_1 (void);  
  7. static void pthread_func_2 (void);  
  8.   
  9. int main (int argc, char** argv)  
  10. {  
  11.   pthread_t pt_1 = 0;  
  12.   pthread_t pt_2 = 0;  
  13.   int ret = 0;  
  14.   
  15.   ret = pthread_create (&pt_1, NULL, pthread_func_1, NULL);  
  16.   if (ret != 0)  
  17.   {  
  18.      perror ("pthread_1_create");  
  19.   }  
  20.   
  21.   ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL);  
  22.   if (ret != 0)  
  23.   {  
  24.      perror ("pthread_2_create");  
  25.   }  
  26.    
  27.   pthread_join (pt_1, NULL);  
  28.   pthread_join (pt_2, NULL);  
  29.   
  30.   return 0;  
  31. }  
  32.   
  33. static void pthread_func_1 (void)  
  34. {  
  35.   int i = 0;  
  36.     
  37.   for (; i < 6; i++)  
  38.   {  
  39.     printf ("This is pthread1!/n");  
  40.   
  41.     if (i == 2)  
  42.     {  
  43.       pthread_exit (0);  
  44.     }  
  45.   
  46.     sleep (1);  
  47.   }  
  48. }  
  49.   
  50. static void pthread_func_2 (void)  
  51. {  
  52.   int i = 0;  
  53.   
  54.   for (; i < 3; i++)  
  55.   {  
  56.     printf (This is pthread2!/n);  
  57.   }  
  58.   
  59.   pthread_exit (0);  
  60. }  

 

在上面的例子中只是简单的创建了线程、主动退出线程和挂起线程。在下一篇文章中,将讲述线程线的属性及其设定。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值