ITPub博客

首页 > IT职业 > IT职场 > 多线程实现多任务二

多线程实现多任务二

原创 IT职场 作者:千锋教育官方 时间:2019-09-17 16:39:00 0 删除 编辑

线程的常用函数

4)回收线程资源

所需头文件:

#include <pthread.h>

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

 

功能:

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

参数:

thread:被等待的线程号。

retval:用来存储线程退出状态的指针的地址。

返回值:

成功:0

失败:非 0

示例代码如下:

#include <stdio.h>  

#include <unistd.h>  

#include <pthread.h>  

  

void *thead(void *arg)  

{  

    static int num = 123; //静态变量  

      

    printf("after 2 seceonds, thread will return\n");  

    sleep(2);  

      

    return #  

}  

  

int main(int argc, char *argv[])  

{  

    pthread_t tid;  

    int ret = 0;  

    void *value = NULL;  

      

    // 创建线程  

    ret = pthread_create(&tid, NULL, thead, NULL);  

    if(ret != 0){ //创建失败  

        perror("pthread_create");  

    }  

      

    // 等待线程号为 tid 的线程,如果此线程结束就回收其资源  

    // &value保存线程退出的返回值  

    pthread_join(tid, &value);   

      

    printf("value = %d\n", *( (int *)value ) );  

      

    return 0;  

}  

运行结果如下:

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

 

所需头文件:

#include <pthread.h>

int pthread_detach(pthread_t thread);

 

功能:

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

参数:

thread:线程号。

返回值:

成功:0

失败:非 0

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

示例代码如下:

#include <stdio.h>  

#include <unistd.h>  

#include <pthread.h>  

  

void *thead(void *arg)  

{  

    int i;  

    for(i=0; i<5; i++)  

    {  

        printf("I am runing\n");  

        sleep(1);  

    }  

      

    return NULL;  

}  

  

int main(int argc, char *argv[])  

{  

    int ret  = 0;  

    pthread_t tid;  

      

    ret = pthread_create(&tid, NULL, thead, NULL);  

    if(ret!=0){  

        perror("pthread_create");  

    }  

      

    pthread_detach(tid); // 线程分离,不阻塞  

      

    // 立马返回,调用失败  

    int flag = pthread_join(tid, NULL);  

    if(flag != 0){  

        printf("join not working\n");  

    }  

      

    printf("after join\n");  

    sleep(3);  

    printf("I am leaving\n");  

      

    return 0;  

}  

 

运行结果如下:

 

5)线程退出

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

所需头文件:

 

#include <pthread.h>

void pthread_exit(void *retval);

 

功能:

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

参数:

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

返回值:

无

示例代码如下:

#include <stdio.h>  

#include <unistd.h>  

#include <pthread.h>  

  

void *thread(void *arg)  

{  

    static int num = 123; //静态变量  

    int i = 0;  

    while(1)  

    {  

        printf("I am runing\n");  

        sleep(1);  

        i++;  

        if(i==3)  

        {  

            pthread_exit( (void *)&num );  

            // return #  

        }  

    }  

      

    return NULL;  

}  

  

int main(int argc, char *argv[])  

{  

    int ret  = 0;  

    pthread_t tid;  

    void *value  = NULL;  

      

    ret = pthread_create(&tid, NULL, thread, NULL);    

    if(ret!=0){  

        perror("pthread_create");  

    }  

      

    pthread_join(tid, &value);  

      

    printf("value = %d\n", *(int *)value );  

      

    return 0;  

}  

 

运行结果如下:

 

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69914734/viewspace-2657289/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
千锋IT职业教育——做真实的自己 用良心做教育

注册时间:2019-03-28

  • 博文量
    140
  • 访问量
    92334