关于Linux 线程pthread_join的用法

关于Linux 线程pthread_join的用法,第1张

Linux系统pthread_join用于挂起当前线程(调用pthread_join的线程),直到thread指定的线程终止运行为止,当前线程才继续执行。

案例代码:

/

    Name:pthread_joinc

    用于Linux下多线程学习

    案例解释线程的暂停和结束

    Author:admin

    Date:2015/8/11       

    Copyright (c) 2015,All Rights Reserved!

#include <pthreadh>

#include <unistdh>

#include <stdioh>

void thread(void str)

{

    int i;

    //不调用pthread_join线程函数

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

    {

        sleep(2);

        printf( "This in the thread : %d\n" , i );

    }

    return NULL;

}

int main()

{

    pthread_t pth;

    int i;

    int ret = pthread_create(&pth, NULL, thread, (void )(i));

    //调用pthread_join线程函数

    pthread_join(pth, NULL);

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

    {

        sleep(1);

        printf( "This in the main : %d\n" , i );

    }

    

    return 0;

}

通过Linux下shell命令执行上面的案例代码:

[root@localhost src]# gcc pthread_joinc -lpthread

[root@localhost src]# /aout

This in the main : 0

This in the thread : 0

This in the main : 1

This in the main : 2

This in the thread : 1

This in the main : 3

This in the main : 4

This in the thread : 2

This in the main : 5

This in the main : 6

This in the thread : 3

This in the main : 7

This in the main : 8

This in the thread : 4

This in the main : 9

子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了,“pthread_join(pth, NULL);”函数起作用。

[root@localhost src]# gcc pthread_joinc -lpthread

[root@localhost src]# /aout

This in the thread : 0

This in the thread : 1

This in the thread : 2

This in the thread : 3

This in the thread : 4

This in the thread : 5

This in the thread : 6

This in the thread : 7

This in the thread : 8

This in the thread : 9

This in the main : 0

This in the main : 1

This in the main : 2

This in the main : 3

This in the main : 4

This in the main : 5

This in the main : 6

This in the main : 7

This in the main : 8

This in the main : 9

这说明pthread_join函数的调用者在等待子线程退出后才继续执行。

/----------------------- beginthreadc ----------------------------/ #include void doubleNum(void arg) { int arg=(int)arg; arg=(arg)2; } void main() { int arg = 0; _beginthread(doubleNum,0,(void)&arg); } /----------------------- pthread_createc ----------------------------/ #include void doubleNum(void arg) { int arg=(int)arg; arg=(arg)2; } void main() { int arg = 0; pthread_t thread; pthread_create(&thread, 0, doubleNum, (void)&arg); pthread_join(thread, NULL); } 2 传递多个参数:定义一个包含所有参数的结构体,传递指向结构体的指针。 /----------------------- StructAndFunch ----------------------------/ #include struct threadparam { int arg1; char[5] arg2; } void doubleNum(void arg) { int arg1=(threadparam)arg->arg1; char arg2=(threadparam)arg->arg2; printf("integer: %d char array: %s \n", arg1, arg2); } /----------------------- beginthreadc ----------------------------/ #include #include void main() { int integer= 5; char[5] array={a,b,c,d,e}; threadparam param=new threadparam(); param->arg1=&interger; param->arg2=array; HANDLE m_hThread = (HANDLE)_beginthread(doubleNum,0,(void)threadparam); } /----------------------- pthread_createc ----------------------------/ #include #include void main() { int integer= 5; char[5] array={a,b,c,d,e}; threadparam param=new threadparam(); param->arg1=&interger; param->arg2=array; pthread_t thread; pthread_create(&thread, 0, doubleNum, (void)param); pthread_join(thread, NULL); }

以上就是关于关于Linux 线程pthread_join的用法全部的内容,包括:关于Linux 线程pthread_join的用法、如何给线程函数 beginthread 和 pthread、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/10159046.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存