【发布时间】:2010-10-31 01:02:30
【问题描述】:
我正在 Cygwin 中进行一个项目。为了尝试在 C 中创建多个线程,并且每个线程使用 system() 函数通过命令行调用另一个可执行文件,结果发现事情没有正常工作。具体来说,我的代码是这样的:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
system("date ");
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
但它不起作用。我得到的错误是堆栈溢出的分段错误。无论如何,对如何通过创建多个线程并行调用系统外壳中的其他可执行文件有一个想法? 谢谢。
【问题讨论】:
-
代码没有明显的问题,在linux上运行良好。作为一个在黑暗中的镜头,您是否与 -pthreads、-lpthreads 等链接?
标签: c cygwin pthreads executable