c - 在 Dev-C++ 中使用 pthreads - 奇怪的错误

标签 c pthreads dev-c++

尝试感受 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

如果您需要让我知道的任何其他规范

提前致谢!

最佳答案

看看您的 printf:格式“%c”期望类型“int”,但参数 2 的类型为“char *”。所以应该是 printf("%s",str);

关于c - 在 Dev-C++ 中使用 pthreads - 奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6075283/

相关文章:

c++ - 如果不知道要读取的字符数,如何使用 fgets?

linux - 检查 fork() 是否安全

c - pthread_cancel 返回 EINPROGRESS

c - 使用并行编程计算总和

c++ - 有什么方法可以增加一个字符吗?

C 套接字编程 -"undefined reference"

java - 用Java编译和运行C程序

C - strtok 返回更少的数据

c - 为什么 fopen 在 main( argv[]) 给定值时失败

c++ - 模板函数只适用于 VS