免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1739 | 回复: 3
打印 上一主题 下一主题

thread problem [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-04 13:41 |只看该作者 |倒序浏览
本帖最后由 zhuizhuziwo 于 2010-12-04 14:15 编辑

what a fucking day.there is no chinese in this system.so i use english. sorry.

I have written a program with two threads. but it has some errors.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. #include <semaphore.h>
  7. #define MAX_LINE_LEN  256

  8. //the second thread.
  9. void * FirstThread(void *arg)
  10. {
  11.     printf("Thread: %s\n", (char *)arg);
  12.     //return EXIT_SUCCESS;
  13.     return EXIT_FAILURE;                              
  14.     //exit(EXIT_SUCCESS);                   //if this thread end with this ,there is no problem.
  15. }

  16. //main thread.
  17. int main()
  18. {
  19.     pthread_t  ptd;
  20.     int rst;
  21.     char str[MAX_LINE_LEN];

  22.     printf("Input string<short<255>:");
  23.     scanf("%s", str);
  24.     rst = pthread_create(&ptd, NULL, FirstThread, (void *)str);
  25.     if(rst != 0)
  26.     {
  27.         //perror("create thread error int %s, %d",__FILE__, __LINE__);
  28.         exit (EXIT_FAILURE);
  29.     }

  30.     void *p_thrdRst;
  31.     rst = pthread_join(ptd, &p_thrdRst);
  32.     if(rst != 0)                                  //last time i forgot this line.
  33.     {
  34.         perror("Error join.\n");
  35.         exit (EXIT_FAILURE);              
  36.     }

  37.     printf("Exit success\n");
  38.     printf("Return string:%s\n", (char *)p_thrdRst);
  39.     return EXIT_SUCCESS;
  40. }
复制代码
the print out has an addtional string:  ": Success", i dont know why?
i run it like this.

Input string<short<255>:whdllo
Thread: whdllo
Error join.
: Success                                //Why??? it seems the perror calls print this message.

论坛徽章:
0
2 [报告]
发表于 2010-12-04 14:16 |只看该作者
i have fixed this problem.thank you all.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2010-12-05 13:36 |只看该作者
what a fucked code

论坛徽章:
0
4 [报告]
发表于 2010-12-06 13:28 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP