linux下怎么在等待线程结束中设置超时

linux下怎么在等待线程结束中设置超时,第1张

有系统调用:pthread_timedjoin_np

The pthread_timedjoin_np() function performs a join-with-timeout. If thread has not yet terminated, then the call blocks until a maximum time, specified in

abstime. If the timeout expires before thread terminates, the call returns an error. The abstime argument is a structure of the following form, specifying

an absolute time measured since the Epoch (see time(2)):

struct timespec {

time_t tv_sec/* seconds */

long tv_nsec /* nanoseconds */

}

姓名:冯成  学号:19020100164  学院:丁香二号书院

转自: https://feixiaoxing.blog.csdn.net/article/details/7240833

【 嵌牛导读 】本文将介绍linux下的C语言开发中的线程等待

【 嵌牛鼻子 】linux C语言 线程等待

【 嵌牛提问 】linux下的C语言开发中的线程等待是什么?

和多进程一样,多线程也有自己的等待函数。这个等待函数就是pthread_join函数。那么这个函数有什么用呢?我们其实可以用它来等待线程运行结束。

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#include <stdlib.h>

void func(void* args)

{

    sleep(2)

    printf("this is func!\n")

}

int main()

{

    pthread_t pid

    if(pthread_create(&pid, NULL, func, NULL))

    {

        return -1

    }

    pthread_join(pid, NULL)

    printf("this is end of main!\n")

    return 0

}

    编写wait.c文件结束之后,我们就可以开始编译了。首先你需要输入gcc wait.c -o wait -lpthread,编译之后你就可以看到wait可执行文件,输入./wait即可。

[test@localhost thread]$ ./thread

this is func!

this is end of main!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7375076.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-04
下一篇2023-04-04

发表评论

登录后才能评论

评论列表(0条)

    保存