【发布时间】:2011-08-29 20:05:51
【问题描述】:
试图感受 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
如果您需要任何其他规格,请让我知道
提前致谢!
【问题讨论】:
-
你为什么使用 DevC++?这是一堆无人维护的小熊维尼。在codeblocks.org使用 Code::Blocks
-
尝试从命令行控制台运行它以查看错误。
-
它不仅没有维护,而且包含严重过时的 gcc (3.x)。
-
我不明白的是人们从哪里得知 DevC++ 是一个很好的 IDE,然后当他们使用它时仍然认为。我记得下载它,运行它并说“你一定是在开玩笑”,然后删除它。
-
甜奶!非常感谢@Neil。 Code::Blocks 运行良好,我的多线程工作正常。泰!