多线程实现多任务

概述

每一个进程都拥有自己的数据段、代码段和堆栈段,这就造成进程在进行创建、切换、撤销操作时,须要较大的系统开销。

为了降低系统开销,从进程中演化出了线程。

为了让进程完毕一定的工作,进程必须至少包括一个线程。线程存在于进程中,共享进程的资源。很多其它详情。请看《进程和线程的差别与联系


就像每一个进程都有一个进程号一样,每一个线程也有一个线程号。进程号在整个系统中是唯一的,但线程号不同,线程号仅仅在它所属的进程环境中有效。进程号用 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


演示样例代码:

  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  ",thread_id);  
  11.   
  12.     if( 0 != pthread_equal( thread_id, pthread_self() ) ){  
  13.         printf("Equal! ");  
  14.     }else{  
  15.         printf("Not equal! ");  
  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() 创建的线程从指定的回调函数開始执行,该函数执行完后,该线程也就退出了。

线程依赖进程存在的,共享进程的资源,假设创建线程的进程结束了。线程也就结束了。


演示样例一:

  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++ ");  
  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 ", 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 ", var);  
  38.         sleep(1);  
  39.     }  
  40.       
  41.     return 0;  
  42. }  

执行结果例如以下:



演示样例二:

  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 ", 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


演示样例代码例如以下:

  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 ");  
  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 ", *( (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() 会立刻返回,调用失败。


演示样例代码例如以下:

  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 ");  
  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 ");  
  33.     }  
  34.       
  35.     printf("after join ");  
  36.     sleep(3);  
  37.     printf("I am leaving ");  
  38.       
  39.     return 0;  
  40. }  

执行结果例如以下:



5)线程退出

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


所需头文件:

#include <pthread.h>


void pthread_exit(void *retval);

功能:

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


參数:

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


返回值:


演示样例代码例如以下:

  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 ");  
  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 ", *(int *)value );  
  38.       
  39.     return 0;  
  40. }  

执行结果例如以下:



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

转自:

原文地址:https://www.cnblogs.com/cynchanpin/p/7223655.html