C++中的多线程中的分段错误(核心转储)

问题描述:

我的代码令我发疯,因为它有时可以正常工作,但有时会发生核心转储或分段错误或双重释放(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)。 –

我只是在猜测这里的问题,但是您将一个(指向a)结构传递给所有线程,并且所有线程都有一些共同的指针,这些指针在结构中共享,例如std::vector。如果多个线程试图同时修改向量,则您有race condition

比赛条件不好,你需要使用某种来防止它们,例如使用a mutex

+0

谢谢你这mush.using互斥体清除了错误; –