1

试图感受 C 中的 pthreads 和多线程编程。感谢这个站点,我已经管理好链接库,这样它就不会在编译时给我带来错误。

我试图让 2 个线程运行,一个打印 1000000 x,一个打印 1000000 o。

但是,在运行程序时会出现问题。命令行弹出一毫秒然后死掉,似乎什么都没有发生。甚至没有任何编译错误或任何我可以修复的东西。如果我注释掉线程创建和线程连接语句,那么命令行会弹出并等待由于系统(“PAUSE”)而被击中的键。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <pthread.h>

void* printChar(void *c);

int main(int argc, char *argv[])
{
    pthread_t thread1;
    pthread_t thread2;

    pthread_create(&thread1,NULL,printChar,"x");
    pthread_create(&thread2,NULL,printChar,"o");

    pthread_join(thread1,NULL);


  system("PAUSE");  
  return 0;
}

void* printChar(void *c)
{
      char *str;
      str = (char*)c;
      int i;
      for(i = 0; i < 1000000; i++)
      {
              printf("%c",str);
      }
}

我正在从 Bloodshed Dev-C++ IDE 运行 DOS C 应用程序。我的 pthread .dll 和 .a 文件版本是:libpthreadGC2.a 和 pthreadGC2.dll

如果您需要任何其他规格 lemmie 知道

提前致谢!

4

1 回答 1

2

看看你的 printf:格式 '%c' 需要类型 'int',但参数 2 的类型是 'char *'。所以应该是 printf("%s",str);

于 2011-05-20T20:32:50.173 回答