2012-10-12 33 views
0

我已經嘗試了很長時間,不知道這'core dumped'來自哪裏。我在cygwin上使用c。註釋掉線程可以解決問題,但註釋掉線程中的整個代碼卻什麼都不做。這可能與線程的調用有關嗎?它似乎在工作,然後突然發生。我刪除了大部分代碼,這是剩下的內容 -c cygwin- abored(core dumped)

#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 

我不知道什麼是錯的! 命令行是 - gcc pi.exe 100 任何組合都會導致此錯誤。 謝謝您的任何洞察力

回答

2

您正在定義'線索'之前分配thread_id。至少應該解決這個問題。

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

是的,你是對的。很久以前,當我清理它的時候,我一定很感動它。我在一段時間內用10作爲測試。這已修復它。 謝謝! – RileyVanZeeland