2015-12-21 166 views
-2

我的代碼令我發瘋,因爲它有時可以正常工作,但有時會發生核心轉儲或分段錯誤或雙重釋放(faststop)錯誤。 我認爲這是因爲一些線程不能創建,但我無法做到。這段代碼有什麼問題? 該代碼應該在存儲在路徑中的文本文件中找到\ n。C++中的多線程中的分段錯誤(核心轉儲)

下面是代碼:

這是Search_inp結構

typedef struct Search_inp{ 
    string * path; 
    string * phrase; 
    int a ; 
    int b; 
    int file_size; 
    vector<int>* vec; 
}search_inp; 

這個功能應該在void *返回指向包含我的數據我想傳遞給線程的結構!

void * make_search_inp(string & path , string & phrase , int a , int b , int file_size , vector<int>&vec){ 
    search_inp * res = (search_inp*)malloc(sizeof(search_inp)); 
    res->path = &path; 
    res->phrase = & phrase; 
    res->a = a; 
    res->b = b; 
    res -> file_size = file_size; 
    res->vec = &vec; 
    return (void *)res; 
} 

此功能將開始搜索\ n的文件

// this function will multi thread the search of \n's and do this through search func 
void find_backslash(string path , vector<int> &place_backslash , int file_size){ 
    int counter = 0; 
    string backslash = "\n"; 
    vector<void*>temp; 
    vector<pthread_t> tid; 
    pthread_t t; 
    while(counter * range <= file_size){ 
     temp.push_back(make_search_inp(path , backslash , counter*range , (counter+1)*range-1 , file_size , place_backslash)); 
     pthread_create(&t, NULL , search , temp.back()); 
     tid.push_back(t); 
     counter++; 
    } 
    for(int i = 0 ; i<tid.size() ;i++) pthread_join(tid[i] , NULL); 
    //when the erorr happend program can not reach this place... 
    while(tid.size()) tid.pop_back(); 
    while(temp.size()) temp.pop_back(); 
    sort(place_backslash.begin() , place_backslash.end()); 

} 

在這是我的代碼搜索功能:

void* search(void * temp){ 
    search_inp* Stemp = (search_inp*)temp; 
    string path = *(Stemp->path); 
    string phrase = *(Stemp->phrase); 
    int a = Stemp->a; 
    int b = Stemp->b; 
    int file_size = Stemp->file_size; 
    vector<int>&vec = *(Stemp->vec); 

    if(path == "") return NULL;//check the path correctness 

    ifstream fin;//1opening the file 2check if the file opening is successful 3put the g in the correct place with seekg 
    fin.open(path.c_str()); 
    if(a < 0) a=0; 
    if(b < 0) b=0; 
    if(a >file_size) 
     a = b = file_size; 
    if(b > file_size){ 
     b = file_size; 
    } 
    fin.seekg(a , fin.beg); 

    if(!fin){ 
     cout << "ERROR:File Does Not Exist!" << endl; 
     return NULL; 
    } 
    //opening the output file for 
    //The search phase 
    int counter=0 , charNum =a;//this counter hold the number of appearance of the phrase in the file 

    while(!fin.eof() && charNum < b){ 
     int cnt = 0;char inp; 
     do{ 
     fin.get(inp);charNum++; 
     if(phrase[cnt] == inp) 
      cnt++; 
     else 
      break; 
     }while(cnt<phrase.length() && !fin.eof()); 
     if(cnt == phrase.length()){ 
      counter++; 
      vec.push_back(((int)fin.tellg())-1); 
     } 
    } 
    fin.close(); 

} 

我會運行這個程序調用find_backslah(path_of_my_file , a vector<int> , size_of_file)並獲得有時會出現錯誤,而且並非總是如此。

+4

請將您的代碼減少到最小但完整的例子。另請參閱本網站的發佈指南。 –

+0

您*可以*嘗試通過在調試器中運行程序來捕捉崩潰。調試器將停在崩潰位置,讓你檢查變量和它們的值,最重要的是讓你檢查和*走上函數調用堆棧,這樣你就可以進入你的代碼(如果調試器沒有'不要停止你的代碼)。如果你仍然無法弄清楚自己,那麼至少告訴我們你的代碼中崩潰發生的地方。 –

+0

另外,你爲什麼混合C和C++這麼多?如果你用C++編程,你應該使用C++類和運算符。因此,而不是使用例如'malloc'來分配內存,你應該使用'new'運算符。在你的代碼中還有其他有問題的東西,比如使用'while(!fin.eof())'這幾乎總是錯誤的,並且使用比指針更健壯的指針(和指針問題在分割時很常見故障)。您可能還想查看['std :: thread'](http://en.cppreference.com/w/cpp/thread/thread)。 –

回答

1

我只是在猜測這裏的問題,但是您將一個(指向a)結構傳遞給所有線程,並且所有線程都有一些共同的指針,這些指針在結構中共享,例如std::vector。如果多個線程試圖同時修改向量,則您有race condition

比賽條件不好,你需要使用某種來防止它們,例如使用a mutex

+0

謝謝你這mush.using互斥體清除了錯誤; –