LinuxSir.cn,穿越时空的Linuxsir!

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

求助:有关多线程的程序

[复制链接]
发表于 2006-3-7 22:48:37 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
void task1(int *count)
{
  while(*count<5)
  {
    printf("task1:%d\n",*count);
    (*count)++;
    sleep(1);
  }
}
void task2(int *count)
{
  while(*count<5)
  {
    printf("task2:%d",*count);
    (*count)++;
  }
}
void cleanup(int a,int b)
{
  printf("total:%d\n",a+b);
}
void task1(int *count);
void task2(int*);
void cleanup(int,int);
int g1=0;
int g2=0;
int main()
{
  pthread_t th1,th2;
  int ret;
  ret=pthread_create(&th1,NULL,(void *)task1,(void *)&g1);
  if(ret)
  {
   perror("task1");
   exit(EXIT_FAILURE);
  }
  ret=pthread_create(&th2,NULL,(void *)task2,(void *)&g2);
  if(ret)
  {
      
    perror("task2");
    exit(EXIT_FAILURE);
  }
  pthread_join(th1,NULL);
  pthread_join(th2,NULL);
  cleanup(g1,g2);
  exit(EXIT_SUCCESS);
}
编译可通过,但是生成的文件无法运行,为什么??
程序有问题吗?
用” gcc -c 文件名 -o 目标 “ 后生成的文件不是可执行文件,并且没有错误提示。不知道是什么原因?
发表于 2006-3-8 20:00:17 | 显示全部楼层
不能运行是什么意思? 错误信息呢?
回复 支持 反对

使用道具 举报

发表于 2006-3-8 23:37:26 | 显示全部楼层
gcc -o prog  prog.c -lpthread
不要用那个 -c 参数。
你这里只是完成了编译,而没有用链接器链接,生成的只是一个.o文件,自然不能运行 。
回复 支持 反对

使用道具 举报

发表于 2006-3-26 15:48:32 | 显示全部楼层
程序上的一点问题.
貌似对线程访问的东西,没有进行加锁哟.
回复 支持 反对

使用道具 举报

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

本版积分规则

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