LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 766|回复: 6

看看这个多线程的程序

[复制链接]
发表于 2004-6-24 16:06:33 | 显示全部楼层 |阅读模式
源码:
#include <stdio.h>
#include <pthread.h>
    void *pp(void *arg)
    {
        while (1) {
            printf("%s\n", (char *)arg);
            sleep(2);
        }
        return NULL;
    }
   
    main()
    {
        pthread_t pid;
        pthread_create(&pid, NULL, pp, "hello world");
        while (1) {
            printf("I am main thread\n");
            sleep(1);
        }
    }

用    gcc test.c -o tt -lpthread
程序可以正常运行.
但是我用g++ test.c -o tt -lpthread出来错误. sleep undeclared....
请问为什么,怎么能解决?
发表于 2004-6-24 16:16:21 | 显示全部楼层
本来就是C程序,用g++编译它干什么?
发表于 2004-6-24 16:48:46 | 显示全部楼层
man 3 sleep
You will need to include unistd.h
#include <unistd.h>
 楼主| 发表于 2004-6-24 16:49:13 | 显示全部楼层
我只是举个简单的例子,我要在c++用多线程的.
如何是下面的呢?
#include <pthread.h>
void *pp(void *arg)
{
while (1) {
   cout<< (char *)arg<<endl;
   sleep(2);
  }
return NULL;
}

main()
{
pthread_t pid;
pthread_create(&pid, NULL, pp, "hello world");
while (1) {
   cout<< (char *)arg<<endl;
sleep(1);
}
}
 楼主| 发表于 2004-6-24 17:25:43 | 显示全部楼层
好了.

很奇怪,怎么有时候用gcc不加<unistd.h>也可以
发表于 2004-6-25 10:23:24 | 显示全部楼层
有时有其他的头文件本身已经有包含unistd.h了
发表于 2004-6-25 20:39:14 | 显示全部楼层
好像是c++要求严格的缘故,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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