鱼C论坛

 找回密码
 立即注册
查看: 3068|回复: 0

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

[复制链接]
发表于 2014-9-17 18:17:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就是一个简单的多线程例子,不在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), 不多说了  大家给看看吧,上代码!!!
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>

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

  13. int main(void)
  14. {
  15.     pthread_t id;
  16.     void** retval;
  17.     printf("process %d thread 0x%x will call pthread_create!\n",getpid(),pthread_se    lf());
  18.     int create_status = pthread_create(&id, NULL, thread_proc,(void*)1);
  19.     if(create_status)
  20.     {
  21.         printf("pthread_create failed!\n");
  22.         return -1;
  23.     }
  24.     printf("pthread_create success, will call pthread_join!\n");
  25.     int join_status = pthread_join(id, retval);
  26.     if(join_status)
  27.     {
  28.         printf("pthread_join failed!\n");
  29.         return -1;
  30.     }
  31.     printf("retval = %d\n", *retval);
  32.     printf("pthread_join return success!\nend main!\n");
  33.     return 0;
  34. }
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 16:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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