免费注册 查看新帖 |

Chinaunix

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

一个线程的程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-12 15:07 |只看该作者 |倒序浏览
#include <stdio.h>
#include <pthread.h>
#define NUM 6
int main()
{
        void print_msg(void* m);

        pthread_t t1,t2;
        pthread_create(&t1,NULL,print_msg,(void *)"hello,");
        pthread_create(&t2,NULL,print_msg,(void *)"whorld!\n");

        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
}

void print_msg(void* m)
{
        char *cp=(char*)m;
        int i;
        for(i=0;i<NUM;i++)
        {
                printf("%s",m);
                fflush(stdout);
                sleep(1);
        }
}

这个程序为什么在我自己的机器上老是运行不起来   总是有警告    并且运行之后也没有输出
以下是报错  恳请大大们帮忙看看  究竟是什么原因老是运行不起来
cc: "99.c", line 9: warning 604: Pointers are not assignment-compatible.
cc: "99.c", line 9: warning 563: Argument #3 is not the correct type.
cc: "99.c", line 10: warning 604: Pointers are not assignment-compatible.
cc: "99.c", line 10: warning 563: Argument #3 is not the correct type.


谢谢了

论坛徽章:
0
2 [报告]
发表于 2007-11-12 15:11 |只看该作者
那个函数,应该返回void *值吧。

论坛徽章:
0
3 [报告]
发表于 2007-11-12 15:20 |只看该作者
线程执行函数返回 void *类型

论坛徽章:
0
4 [报告]
发表于 2007-11-12 15:45 |只看该作者
改了还是有警告啊

论坛徽章:
0
5 [报告]
发表于 2007-11-12 15:46 |只看该作者
貌似错就错在线程执行函数的哪个参数那  我就是不知道怎么改好啊

论坛徽章:
0
6 [报告]
发表于 2007-11-12 15:50 |只看该作者

回复 #5 wangxing8818 的帖子

不是那,是函数的返回类型。

论坛徽章:
0
7 [报告]
发表于 2007-11-12 16:00 |只看该作者
好好看看我给你改了几处?


  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>

  4. #define NUM 6

  5. int main()
  6. {
  7.         void* print_msg(void* m);

  8.         pthread_t t1,t2;
  9.         pthread_create(&t1,NULL,print_msg,(void *)"hello,");
  10.         pthread_create(&t2,NULL,print_msg,(void *)"whorld!\n");

  11.         pthread_join(t1,NULL);
  12.         pthread_join(t2,NULL);

  13.         return 0;
  14. }

  15. void* print_msg(void* m)
  16. {
  17.         char *cp=(char*)m;
  18.         int i;
  19.         for(i=0;i<NUM;i++)
  20.         {
  21.                 printf("%s",cp);
  22.                 fflush(stdout);
  23.                 sleep(1);
  24.         }
  25.   return NULL;
  26. }

复制代码

论坛徽章:
0
8 [报告]
发表于 2007-11-12 16:36 |只看该作者
能讲一下为什么要这么改吗    还有 就是这样改过之后  还是printf不出来啊

论坛徽章:
0
9 [报告]
发表于 2007-11-12 16:42 |只看该作者
原帖由 wangxing8818 于 2007-11-12 16:36 发表
能讲一下为什么要这么改吗    还有 就是这样改过之后  还是printf不出来啊


我的怎么能打印出来?
gcc tmp.c -Wall -lpthread

$ ./a.out
hello,whorld!
hello,whorld!
hello,whorld!
hello,whorld!
hello,whorld!
hello,whorld!

论坛徽章:
0
10 [报告]
发表于 2007-11-12 17:05 |只看该作者
sub-test/report/app/testing/wang> cc 99.c
sub-test/report/app/testing/wang> a.out
sub-test/report/app/testing/wang>

真打印不出来  我郁闷了  我在printf("%s",cp);前面加一句printf("%d",i)
连i都不能打印的
貌似我这里好象连子函数都没有调用成功
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP