ascxyh009 发表于 2014-9-17 18:17:29

大家来帮忙看看是个什么问题?我都晕了

就是一个简单的多线程例子,不在printf函数中调用getpid()retval就能正常接收到线程函数的返回,加上(如下代码)之后就不能就收到了(retval == NULL), 分成两个printf分别打印pid和thread_id也没问题,彻底晕了,我用的是Debian3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux(uname -a), 不多说了大家给看看吧,上代码!!!
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

void* thread_proc(void* arg)
{
    printf("process %d begin thread 0x%x!\ndealing...\n",getpid(),pthread_self());
    sleep(5);
    printf("arg = %d\nend thread!\n", arg);
    return arg;
}

int main(void)
{
    pthread_t id;
    void** retval;
    printf("process %d thread 0x%x will call pthread_create!\n",getpid(),pthread_se    lf());
    int create_status = pthread_create(&id, NULL, thread_proc,(void*)1);
    if(create_status)
    {
      printf("pthread_create failed!\n");
      return -1;
    }
    printf("pthread_create success, will call pthread_join!\n");
    int join_status = pthread_join(id, retval);
    if(join_status)
    {
      printf("pthread_join failed!\n");
      return -1;
    }
    printf("retval = %d\n", *retval);
    printf("pthread_join return success!\nend main!\n");
    return 0;
}



页: [1]
查看完整版本: 大家来帮忙看看是个什么问题?我都晕了