Linux下POSIX Pthread线程库的使用

   上学期学习了操作系统课程,今天简单复习一下POSIX Pthread的使用。

一、前言

  1.目前主要的线程库

       (1)POSIX Pthread:提供用户级或内核级的库

       (2)Win32:内核级库,嵌入到内核的方式

       (3)Java:Java线程API通常采用宿主系统上的线程库来实现

  2.使用多线程的优点

       (1)运行于一个进程中的多个线程,使用相同的地址空间,共享大部分数据。

              启动线程需要的空间,切换线程的时间花费 都要少很多

       (2)线程间方便的通信机制(但也存在着共享数据的问题)

  3.POSIX Pthread 的常用API

        (1)/** 

                 * 创建线程系统调用

                 * @param  *tidp: 线程id指针

                 * @param  *attr: 设置线程属性

                 * @param  (*start_rtn)(void*): 线程

                 * @param  *arg: 线程参数

                 * @return 线程创建成功 0;线程创建失败 错误编号

                 */                                                

                 int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);

        

       (2)/** 

                 * 终止线程执行       

                 */   

                void pthread_exit();

          

         (3)  /**

                 * 等待一个线程结束

                 * @param thread 线程id

                 * @param **retval 存储线程返回的值

                 * @return 成功 0 ;失败 返回错误号 

                 */

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

  二、实例                     

    1.准备工作

        (1)编写Linux下的多线程程序,需要引入头文件  #include

        (2)使用bash脚本循环运行本实例

    2.example_1

#include
#include


void* runnerC (void)
{
    printf("[thread 3] this is C.\n");
}


void* runnerB (void)
{
    printf("[thread 2] this is B.\n");
}


int main(void)
{
    pthread_t tid_B;
    pthread_t tid_C;
    
    int ret1,ret2;


    ret1 = pthread_create(&tid_C,NULL,runnerC,NULL);
    if(ret1 != 0)
    {
       printf("thread_C is wrong!");
       exit(0);
    }
    
    ret2 = pthread_create(&tid_B,NULL,runnerB,NULL);
    if(ret2 != 0)
    {
       printf("thread_B is wrong!");
       exit(0);
    }
    
    printf("[thread 1] this is A.\n");


    return(0);    
}

结果为:

1
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
2
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is B.
3
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
4
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
5
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
6
[thread 1] this is A.
[thread 2] this is C.
[thread 3] this is C.
7
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
8
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
9
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is B.
10
[thread 1] this is A.
[thread 2] this is B.

[thread 3] this is C.

再次运行 结果可能为

1
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
2
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
3
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
4
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
5
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
6
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
7
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
8
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
9
[thread 1] this is A.
[thread 2] this is B.
[thread 3] this is C.
10
[thread 1] this is A.
[thread 2] this is B.

[thread 3] this is C.

   可以看到 多次运行后 输出的结果和预想的 ABC 按顺序输出 出现了不一样 
   这就是多个线程在争夺CPU资源时 在某个时间点 CPU资源被哪个线程占用是不确定的 

   为了让线程按规定好的顺序运行,在这次实例中采用函数 pthread_join 来控制

  3.example_2

    pthread_join(tid_B,NULL);

    pthread_join(tid_C,NULL);

经过多次运行,ABC均按顺序输出

4.总结

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


你可能感兴趣的:(Linux下POSIX Pthread线程库的使用)