多线程实现多任务

来源:互联网 发布:建模 软件p 编辑:程序博客网 时间:2024/05/29 06:58

概述

每个进程都拥有自己的数据段、代码段和堆栈段,这就造成进程在进行创建、切换、撤销操作时,需要较大的系统开销。为了减少系统开销,从进程中演化出了线程。为了让进程完成一定的工作,进程必须至少包含一个线程。线程存在于进程中,共享进程的资源。更多详情,请看《进程和线程的区别与联系


就像每个进程都有一个进程号一样,每个线程也有一个线程号。进程号在整个系统中是唯一的,但线程号不同,线程号只在它所属的进程环境中有效。进程号用 pid_t 数据类型表示,是一个非负整数。线程号则用 pthread_t 数据类型来表示,Linux 使用无符号长整数表示。有的系统在实现 pthread_t 的时候,用一个结构体来表示,所以在可移植的操作系统实现不能把它做为整数处理。


线程的常用函数

1)获取线程号

所需头文件:

#include <pthread.h>


pthread_t pthread_self(void);

功能:

获取线程号。

参数:

返回值:

调用线程的线程 ID 。


2)线程号的比较

所需头文件:

#include <pthread.h>


int pthread_equal(pthread_t t1, pthread_t t2);

功能:

判断线程号 t1 和 t2 是否相等。为了方便移植,尽量使用函数来比较线程 ID。

参数:

t1,t2:待判断的线程号。

返回值:

相等:  非 0

不相等:0


示例代码:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <pthread.h>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     pthread_t thread_id;  
  8.   
  9.     thread_id = pthread_self(); // 返回调用线程的线程ID  
  10.     printf("Thread ID = %lu \n",thread_id);  
  11.   
  12.     if( 0 != pthread_equal( thread_id, pthread_self() ) ){  
  13.         printf("Equal!\n");  
  14.     }else{  
  15.         printf("Not equal!\n");  
  16.     }  
  17.       
  18.     return 0;  
  19. }  

线程函数的程序在 pthread 库中,故链接时要加上参数 -lpthread


运行结果如下:



3)线程的创建

所需头文件:

#include <pthread.h>


int pthread_create( pthread_t *thread,

const pthread_attr_t *attr,

void *(*start_routine)(void *),

void *arg );

功能:

创建一个线程。

参数:

thread:线程标识符地址。

attr:线程属性结构体地址,通常设置为 NULL。

start_routine:线程函数的入口地址。

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

返回值:

成功:0

失败:非 0


pthread_create() 创建的线程从指定的回调函数开始运行,该函数运行完后,该线程也就退出了。线程依赖进程存在的,共享进程的资源,如果创建线程的进程结束了,线程也就结束了。


示例一:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <pthread.h>  
  4.   
  5. int var  = 8;  
  6.   
  7. void *thread_1(void *arg)  
  8. {  
  9.     while(1)  
  10.     {  
  11.         printf("this is my new thread1: var++\n");  
  12.         var++;  
  13.         sleep(1);  
  14.     }  
  15.     return NULL;  
  16. }  
  17.   
  18. void *thread_2(void * arg)  
  19. {  
  20.     while(1){  
  21.         printf("this is my new thread2: var = %d\n", var);  
  22.         sleep(1);  
  23.     }  
  24.       
  25.     return NULL;  
  26. }  
  27.   
  28. int main(int argc, char *argv[])  
  29. {  
  30.     pthread_t tid1,tid2;  
  31.       
  32.     //创建两个线程  
  33.     pthread_create(&tid1, NULL, thread_1, NULL);    
  34.     pthread_create(&tid2, NULL, thread_2, NULL);  
  35.       
  36.     while(1){  
  37.         printf("the main thread: var = %d\n", var);  
  38.         sleep(1);  
  39.     }  
  40.       
  41.     return 0;  
  42. }  

运行结果如下:



示例二:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <pthread.h>  
  4.   
  5. // 回调函数  
  6. void *thread_fun(void * arg)  
  7. {  
  8.     sleep(1);  
  9.     int num = *( (int *)arg );  
  10.     printf("int the new thread: num = %d\n", num);  
  11.       
  12.     return NULL;  
  13. }  
  14.   
  15. int main(int argc, char *argv[])  
  16. {  
  17.     pthread_t tid;  
  18.     int test = 100;  
  19.       
  20.     // 创建线程, 把 &test 传给回调函数 thread_fun()  
  21.     pthread_create(&tid, NULL, thread_fun, (void *)&test);    
  22.   
  23.     while(1);  
  24.       
  25.     return 0;  
  26. }  

运行结果如下:



4)回收线程资源

所需头文件:

#include <pthread.h>


int pthread_join(pthread_t thread, void **retval);

功能:

等待线程结束(此函数会阻塞),并回收线程资源,类似进程的 wait() 函数。如果线程已经结束,那么该函数会立即返回。

参数:

thread:被等待的线程号。
retval:用来存储线程退出状态的指针的地址。

返回值:

成功:0

失败:非 0


示例代码如下:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <pthread.h>  
  4.   
  5. void *thead(void *arg)  
  6. {  
  7.     static int num = 123; //静态变量  
  8.       
  9.     printf("after 2 seceonds, thread will return\n");  
  10.     sleep(2);  
  11.       
  12.     return #  
  13. }  
  14.   
  15. int main(int argc, char *argv[])  
  16. {  
  17.     pthread_t tid;  
  18.     int ret = 0;  
  19.     void *value = NULL;  
  20.       
  21.     // 创建线程  
  22.     ret = pthread_create(&tid, NULL, thead, NULL);  
  23.     if(ret != 0){ //创建失败  
  24.         perror("pthread_create");  
  25.     }  
  26.       
  27.     // 等待线程号为 tid 的线程,如果此线程结束就回收其资源  
  28.     // &value保存线程退出的返回值  
  29.     pthread_join(tid, &value);   
  30.       
  31.     printf("value = %d\n", *( (int *)value ) );  
  32.       
  33.     return 0;  
  34. }  

运行结果如下:



创建一个线程后应回收其资源,但使用 pthread_join() 函数会使调用者阻塞,Linux 还提供了非阻塞函数 pthread_detach() 来回收线程的资源。


所需头文件:

#include <pthread.h>


int pthread_detach(pthread_t thread);

功能:

使调用线程与当前进程分离,分离后不代表此线程不依赖与当前进程,线程分离的目的是将线程资源的回收工作交由系统自动来完成,也就是说当被分离的线程结束之后,系统会自动回收它的资源。所以,此函数不会阻塞。

参数:

thread:线程号。

返回值:

成功:0

失败:非 0


注意,调用 pthread_detach() 后再调用 pthread_join() , pthread_join() 会立马返回,调用失败。


示例代码如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <pthread.h>  
  4.   
  5. void *thead(void *arg)  
  6. {  
  7.     int i;  
  8.     for(i=0; i<5; i++)  
  9.     {  
  10.         printf("I am runing\n");  
  11.         sleep(1);  
  12.     }  
  13.       
  14.     return NULL;  
  15. }  
  16.   
  17. int main(int argc, char *argv[])  
  18. {  
  19.     int ret  = 0;  
  20.     pthread_t tid;  
  21.       
  22.     ret = pthread_create(&tid, NULL, thead, NULL);  
  23.     if(ret!=0){  
  24.         perror("pthread_create");  
  25.     }  
  26.       
  27.     pthread_detach(tid); // 线程分离,不阻塞  
  28.       
  29.     // 立马返回,调用失败  
  30.     int flag = pthread_join(tid, NULL);  
  31.     if(flag != 0){  
  32.         printf("join not working\n");  
  33.     }  
  34.       
  35.     printf("after join\n");  
  36.     sleep(3);  
  37.     printf("I am leaving\n");  
  38.       
  39.     return 0;  
  40. }  

运行结果如下:



5)线程退出

在进程中我们可以调用 exit() 函数或 _exit() 函数来结束进程,在一个线程中我们可以通过 pthread_exit() 在不终止整个进程的情况下停止它的控制流。


所需头文件:

#include <pthread.h>


void pthread_exit(void *retval);

功能:

退出调用线程。一个进程中的多个线程是共享该进程的数据段,因此,通常线程退出后所占用的资源并不会释放。

参数:

retval:存储线程退出状态的指针。

返回值:


示例代码如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <pthread.h>  
  4.   
  5. void *thread(void *arg)  
  6. {  
  7.     static int num = 123; //静态变量  
  8.     int i = 0;  
  9.     while(1)  
  10.     {  
  11.         printf("I am runing\n");  
  12.         sleep(1);  
  13.         i++;  
  14.         if(i==3)  
  15.         {  
  16.             pthread_exit( (void *)&num );  
  17.             // return &num;  
  18.         }  
  19.     }  
  20.       
  21.     return NULL;  
  22. }  
  23.   
  24. int main(int argc, char *argv[])  
  25. {  
  26.     int ret  = 0;  
  27.     pthread_t tid;  
  28.     void *value  = NULL;  
  29.       
  30.     ret = pthread_create(&tid, NULL, thread, NULL);    
  31.     if(ret!=0){  
  32.         perror("pthread_create");  
  33.     }  
  34.       
  35.     pthread_join(tid, &value);  
  36.       
  37.     printf("value = %d\n", *(int *)value );  
  38.       
  39.     return 0;  
  40. }  

运行结果如下:



本教程示例代码下载请点此链接。

转自:

0 0