3.1、线程基本编程

  为了进一步减少处理器的空转时间,支持多处理器以及减少上下文切换开销,进程在演化中出现了另一个概念-----线程。它是进程内独立的一条运行路线,是内核调度的最小单元,也被称为轻量级进程。线程由于具有高效性和可操作性,在嵌入式系统开发中运用的非常广泛,希望读者能够很好地掌握。  

  这里要讲的线程相关操作都是用户空间中的线程的操作。在Linux中,一般pthread线程库是一套通用的线程库,是由POSIX提出的,因此具有很好的可移植性。

  pthread线程库是第三方库,不是标准的C库。代码中有第三方库是,在编译时要在后面加上库名(-lpthread或-pthread),以显示连接该库,如gcc main.c -pthread。

  线程通过全局变量和堆区来进行通信。

1、函数说明

  创建线程的流程:创建线程----->执行相应的线程函数---->线程结束

  创建线程实际上就是确定调用该线程时线程函数的入口点,这里通常使用的函数是pthread_create()

  在线程创建以后,就开始运行相关的线程函数,在该函数运行完之后,该线程也就退出了,这也是线程退出一种方法。

  另一种退出线程的方法是使用函数pthread_exit(),这是线程的主动行为。这里要注意的是,在使用线程函数时,不能随意使用exit()退出函数进行出错处理,由于exit()的作用是使进程终止,往往一个进程包含多个线程,因此,在使用exit()之后,该进程中的所有线程都终止了。因此,在线程中就可以使用pthread_exit()来代替进程中的exit()。

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

  前面已提到线程调用pthread_exit()函数主动终止自身线程。但是在很多线程应用中,经常会遇到在别的线程中要终止另一个线程的执行的问题。此时调用pthread_cancel()函数实现这种功能,但在被取消的线程的内部需要调用pthread_setcancel()函数和pthread_setcanceltype()函数设置自己的取消状态,例如被取消的线程接收到另一个线程的取消请求之后,是接受还是忽略这个请求;如果接受,是立刻进行终止操作还是等待某个函数的调用等。

2、函数语法

头文件

#include <pthread.h>

函数原型

int pthread_create ((pthread_t *thread, pthread_attr_t *attr,void *(*start_routine)(void *),void *arg));

作用

创建线程

参数

thread

线程标识符,指向线程标识符的指针,线程号(ID)

attr

线程属性设置,NULL表示缺省属性

start_routine

线程处理函数,参数和返回值都是void*

arg

传递给线程处理函数start_routine的参数

返回值

成功

0

失败

返回错误码

 

头文件

#include <pthread.h>

函数原型

void pthread_exit(void *retval);

作用

结束线程

参数

retval

线程结束时的返回值,可由其他函数如pthread_join()来获取

返回值

 

头文件

#include <pthread.h>

函数原型

int pthread_join ((pthread_t th, void **thread_return));

作用

该函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源就被收回。

参数

th

等待线程的标识符

thread_return

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

返回值

成功

0

失败

返回错误码

 

头文件

#include <pthread.h>

函数原型

int pthread_cancel((pthread_t th);

作用

在一个线程中去终止另一个线程

参数

th

要取消的线程的标识符

返回值

成功

0

失败

返回错误码

 

3、函数使用

以下实例中创建了3个线程,为了更好地描述线程之间的并行执行,让3个线程重用同一个执行函数。每个线程都有5次循环(可以看成5个小任务),每次循环之间会随机等待1~10s的时间,意义在于模拟每个任务的到达时间是随机的,并没有任何特定规律。

/* thread.c */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define THREAD_NUMBER 3 /*线程数*/
#define REPEAT_NUMBER 5 /*每个线程中的小任务数*/
#define DELAY_TIME_LEVELS 10.0 /*小任务之间的最大时间间隔*/

void *thrd_func(void *arg) {   int thrd_num = (int)arg;   int delay_time = 0;   int count = 0;   printf("Thread %d is startingn", thrd_num);   for (count = 0; count < REPEAT_NUMBER; count++)   {     delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;     sleep(delay_time);     printf("tThread %d: job %d delay = %dn",     thrd_num, count, delay_time);   }   printf("Thread %d finishedn", thrd_num);   pthread_exit(NULL); } int main(void) {   pthread_t thread[THREAD_NUMBER];   int no = 0, res;   void * thrd_ret;   srand(time(NULL));   for (no = 0; no < THREAD_NUMBER; no++)   {     /* 创建多线程 */     res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);     if (res != 0)     {       printf("Create thread %d failedn", no);       exit(res);     }   }   printf("Create treads successn Waiting for threads to finish...n");   for (no = 0; no < THREAD_NUMBER; no++)   {     /* 等待线程结束 */     res = pthread_join(thread[no], &thrd_ret);     if (!res)     {       printf("Thread %d joinedn", no);     }     else     {       printf("Thread %d join failedn", no);     }
  }   
return 0; }
以下是程序运行结果。可以看出每个线程的运行和结束是独立与并行的。
$ ./thread
Create treads success
Waiting for threads to finish...
Thread 0 is starting
Thread 1 is starting
Thread 2 is starting
Thread 1: job 0 delay = 6
Thread 2: job 0 delay = 6
Thread 0: job 0 delay = 9
Thread 1: job 1 delay = 6
Thread 2: job 1 delay = 8
Thread 0: job 1 delay = 8
Thread 2: job 2 delay = 3
Thread 0: job 2 delay = 3
Thread 2: job 3 delay = 3
Thread 2: job 4 delay = 1
Thread 2 finished
Thread 1: job 2 delay = 10
Thread 1: job 3 delay = 4
Thread 1: job 4 delay = 1
Thread 1 finished
Thread 0: job 3 delay = 9
Thread 0: job 4 delay = 2
Thread 0 finished
Thread 0 joined
Thread 1 joined
Thread 2 joined

 

posted @ 2020-02-11 15:33  孤情剑客  阅读(267)  评论(0)    收藏  举报