搜索
bottom↓
回复: 7

请问各位linux里pthread_join(*(pthread_t*)arg, NULL);这句话代表什么意思?

[复制链接]

出0入0汤圆

发表于 2012-1-11 10:06:20 | 显示全部楼层 |阅读模式
void* thread1(void* arg)
{
        printf("enter thread1\n");
        printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_lock(&mutex);
        if(g_Flag == 2)
                pthread_cond_signal(&cond);
        g_Flag = 1;
        printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_unlock(&mutex);
        pthread_join(*(pthread_t*)arg, NULL);
        printf("leave thread1\n");
        pthread_exit(0);
}
如题,有这么一个线程,可不知道pthread_join(*(pthread_t*)arg, NULL);这句话代表什么意思,哪位大虾可以告知一下,不胜感激!

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2012-1-11 10:48:34 | 显示全部楼层
函数pthread_join用来等待一个线程的结束

出0入0汤圆

 楼主| 发表于 2012-1-11 11:49:45 | 显示全部楼层
回复【1楼】ljt80158015  
-----------------------------------------------------------------------

这个我知道,可里面*(pthread_t*)arg这个参数我不知道代表什么意思?

出0入0汤圆

发表于 2012-1-11 13:19:42 | 显示全部楼层
回复【2楼】w282529350  馒头
回复【1楼】ljt80158015  
-----------------------------------------------------------------------
这个我知道,可里面*(pthread_t*)arg这个参数我不知道代表什么意思?
-----------------------------------------------------------------------

arg 是线程thread1传递进来的

出0入0汤圆

 楼主| 发表于 2012-1-11 14:15:54 | 显示全部楼层
回复【3楼】ljt80158015  
-----------------------------------------------------------------------
谢谢您的回答,我把完整的程序贴出来,您帮我看下吧,您说“arg 是线程thread1传递进来的”,那这个参数代表什么意思?是不是等待线程2结束的意思?



1)有一int型全局变量g_Flag初始值为0;
  2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
  3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
  4)线程序1需要在线程2退出后才能退出
  5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
   */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>

typedef void* (*fun)(void*);

int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* thread1(void*);
void* thread2(void*);

/*
*  when program is started, a single thread is created, called the initial thread or main thread.
*  Additional threads are created by pthread_create.
*  So we just need to create two thread in main().
*/

int main(int argc, char** argv)
{
        printf("enter main\n");
        pthread_t tid1, tid2;
        int rc1=0, rc2=0;
        rc2 = pthread_create(&tid2, NULL, thread2, NULL);
        if(rc2 != 0)
                printf("%s: %d\n",__func__, strerror(rc2));

        rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
        if(rc1 != 0)
                printf("%s: %d\n",__func__, strerror(rc1));

        pthread_cond_wait(&cond, &mutex);
        printf("leave main\n");
        exit(0);       
}

/*
* thread1() will be execute by thread1, after pthread_create()
* it will set g_Flag = 1;
*/
void* thread1(void* arg)
{
        printf("enter thread1\n");
        printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_lock(&mutex);
        if(g_Flag == 2)
                pthread_cond_signal(&cond);
        g_Flag = 1;
        printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_unlock(&mutex);
        pthread_join(*(pthread_t*)arg, NULL);
        printf("leave thread1\n");
        pthread_exit(0);
}

/*
* thread2() will be execute by thread2, after pthread_create()
* it will set g_Flag = 2;
*/
void* thread2(void* arg)
{
        printf("enter thread2\n");
        printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_lock(&mutex);
        if(g_Flag == 1)
                pthread_cond_signal(&cond);
        g_Flag = 2;
        printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
        pthread_mutex_unlock(&mutex);
        printf("leave thread2\n");
        pthread_exit(0);
}
编译运行可以得到符合要求的结果!

enter main
enter thread2
this is thread2, g_Flag: 0, thread id is 3086408592
this is thread2, g_Flag: 2, thread id is 3086408592
leave thread2
enter thread1
this is thread1, g_Flag: 2, thread id is 3075918736
this is thread1, g_Flag: 1, thread id is 3075918736
leave thread1
leave main

出0入0汤圆

 楼主| 发表于 2012-1-11 15:39:42 | 显示全部楼层
回复【3楼】ljt80158015  
-----------------------------------------------------------------------

再次感谢!我懂了。rc1 = pthread_create(&tid1, NULL, thread1, &tid2); 这里传递参数的,怪我看代码太不仔细了.....

出0入0汤圆

发表于 2012-1-11 15:41:13 | 显示全部楼层
pthread_join用来等待线程结束并释放线程占用的资源。

出0入0汤圆

 楼主| 发表于 2012-1-11 15:50:15 | 显示全部楼层
回复【6楼】kelp  
-----------------------------------------------------------------------

谢谢 一般都是第一个参数写进程的ID,这个程序里写的传递来的参数,以为是新用法....
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 23:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表