0

我已经尝试了很长时间,无法弄清楚这'core dumped'是从哪里来的。我正在c使用cygwin. 注释掉线程可以解决问题,但是注释掉线程中的整个代码什么都不会。这可能与线程的调用有关吗?它似乎正在工作,然后这突然发生了。我已经删除了大部分代码,这就是剩下的 -

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <pthread.h>
#include <string.h>


typedef enum {true=1, false=0} bool;

void *piThread(void *arg);
int finished;

int main(int argc, char *argv[])
{
    int i;
    int threads;
    bool display = false;
    long double pI = 0.0;
    void *status = malloc(sizeof(int));
    pthread_t thread_id[threads];

    if(argc < 2) {printf("not enough arguments"); exit(1);
    }else threads = atoi(argv[1]);

    if(argc == 3)
        if (strcmp(argv[2], "b") == 0)
            display = true;

    for(i=0; i<threads; i++)
    {
            pthread_create(&thread_id[i], NULL, piThread, NULL);    
            pthread_join(thread_id[i], &status);
            printf("pi: %Lf\n", pI);
    }
    return 0;
}

void *piThread(void *arg)
{
    int number = 0;
    number = 74;
    pthread_exit((void*)number);
}

这导致中止错误。

Stack trace:
Frame     Function  Args
0028A6A4  76821184  (000000D0, 0000EA60, 00000000, 0028A7D8)
0028A6B8  76821138  (000000D0, 0000EA60, 000000A4, 0028A7B4)
0028A7D8  610DBE29  (00000000, FFFFFFFE, 77403B23, 77403B4E)
0028A8C8  610D915E  (00000000, 0028A918, 00000001, 00000000)
0028A928  610D962E  (76D709CD, 7427AED9, 00000003, 00000006)
0028A9D8  610D9780  (000011E8, 00000006, 002B002B, 800483D8)
0028A9F8  610D97AC  (00000006, 0028CE80, FFFDE000, 00000000)
0028AA28  610D9A85  (000000D0, 0028ABF0, 0028AA58, 610FA223)
End of stack trace

我不知道出了什么问题!!命令行 is- gcc pi.exe 100 26 以上的任何组合都会导致此故障。感谢您的任何见解

4

1 回答 1

2

You are allocating thread_id before 'threads' is defined. This should fix that problem at least.

if(argc < 2) {printf("not enough arguments"); exit(1);
}else threads = atoi(argv[1]);
pthread_t thread_id[threads];
于 2012-10-12T12:14:59.797 回答