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

Segmentation fault(core dumped) in multi threading in c++

我的代码让我抓狂,因为它有时工作正常但有时会出现核心转储或分段错误或双重释放(faststop)错误。 我认为这是因为无法创建某些线程但我无法创建它。这段代码有什么问题? 此代码应该在存储在路径中的文本文件中找到 \n。

代码如下:

这是Search_inp结构

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

这个函数应该return一个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) 并且有时会出现错误,但并非总是如此。

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

竞争条件很糟糕,您需要使用某种 lock 来防止它们,例如使用 a mutex.