Linux多线程编程(创建线程)

  • 创建线程

创建线程的函数是pthread_create,具体定义如下:

[cpp]  view plain copy
  1. #include <pthread.h>   
  2.   
  3. int pthread_create(pthread_t *thread,                 // 新创建的线程ID   
  4.                    const pthread_attr_t *attr,        // 线程属性   
  5.                    void *(*start_routine) (void *),   // 新创建的线程从start_routine开始执行     
  6.                    void *arg);                        // 执行函数的参数   
 创建线程函数使用示例:

[cpp]  view plain copy
  1. /* example.c*/  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4. #include <unistd.h>  
  5.   
  6. void thread()  
  7. {  
  8.     int i = 0;  
  9.       
  10.     for(i=0;i<3;i++)  
  11.     {  
  12.         printf("This is the new pthread (%d).\n", i);  
  13.         sleep(1);  
  14.     }  
  15. }  
  16.   
  17. int main(int argc, char *argv[])  
  18. {  
  19.     pthread_t id;  
  20.     int ret = 0;  
  21.       
  22.     ret=pthread_create(&id, NULL, (void *) thread, NULL);  
  23.     if(ret!=0)  
  24.     {  
  25.         printf ("Create pthread error!\n");  
  26.         return 0;  
  27.     }  
  28.       
  29.     printf("This is the main thread.\n");  
  30.     
  31.     return 0;  
  32. }  
使用如下命令编译:
gcc -o newthread newthread.c
发现编译不通过,报错如下:
[yuanping@Linux C]$ gcc -o newthread -g newthread.c
/tmp/ccKagF7L.o: In function `main':
newthread.c:(.text+0x7c): undefined reference to `pthread_create'
newthread.c:(.text+0xe5): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
[yuanping@Linux C]$

报这个错误的原因是:pthread库不是linux默认的库,所以在编译时候需要指明libpthread.a库。
解决方法:在编译时,加上-lpthread参数。
使用如下命令编译成功:
gcc -lpthread -o newthread -g newthread.c
执行结果:
[yuanping@Linux C]$ newthread 
This is the main thread.
[yuanping@Linux C]$
 
上述结果并非预期的结果,预期的结果应该新创建的线程也打印 This is the new thread.
出现此问题的原因是主线程没有等待子线程正常退出,主线程运行结束退出时,所有的子线程都退出了。下面介绍通过pthread_join函数等待子线程运行结束。


  • 等待线程退出

[cpp]  view plain copy
  1. #include <pthread.h>   
  2.   
  3. int pthread_join(pthread_t thread,  // 等待退出的线程ID   
  4.                  void **retval);    // 获取线程退出时的返回值   
对前面的程序进行改进,添加pthread_join(id, NULL);如下

[cpp]  view plain copy
  1. /* example.c*/  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4. #include <unistd.h>  
  5.   
  6. void thread()  
  7. {  
  8.     int i = 0;  
  9.       
  10.     for(i=0;i<3;i++)  
  11.     {  
  12.         printf("This is the new pthread (%d).\n", i);  
  13.         sleep(1);  
  14.     }  
  15. }  
  16.   
  17. int main(int argc, char *argv[])  
  18. {  
  19.     pthread_t id;  
  20.     int ret = 0;  
  21.       
  22.     ret=pthread_create(&id, NULL, (void *) thread, NULL);  
  23.     if(ret!=0)  
  24.     {  
  25.         printf ("Create pthread error!\n");  
  26.         return 0;  
  27.     }  
  28.       
  29.     printf("This is the main thread.\n");  
  30.   
  31.     pthread_join(id, NULL);  
  32.       
  33.     return 0;  
  34. }  
编译运行结果如下:
[yuanping@Linux C]$ gcc -lpthread -o newthread -g newthread.c
[yuanping@Linux C]$ newthread 
This is the main thread.
[yuanping@Linux C]$ gcc -lpthread -o newthread -g newthread.c 
[yuanping@Linux C]$ newthread 
This is the main thread.
This is the new pthread (0).
This is the new pthread (1).
This is the new pthread (2).
[yuanping@Linux C]$
 

运行结果与预期的结果一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值