Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尽管存在互斥锁,线程中的争用条件仍然存在_C++_Multithreading_Pthreads_Race Condition - Fatal编程技术网

C++ 尽管存在互斥锁,线程中的争用条件仍然存在

C++ 尽管存在互斥锁,线程中的争用条件仍然存在,c++,multithreading,pthreads,race-condition,C++,Multithreading,Pthreads,Race Condition,这段代码计算数组中所有整数的总和,在多个线程之间平均分配工作量。但是,每隔一段时间,线程数以及线程的localsum都会出错。我假设这是因为void*param和globalindex是由多个线程同时访问的。没有意义的是,尽管我将代码中的每个全局变量都设为mutexlock,但它还是发生了。 我该如何解决这个问题 #include<string> #include<iostream> #include<fstream> #include<unistd.h

这段代码计算数组中所有整数的总和,在多个线程之间平均分配工作量。但是,每隔一段时间,线程数以及线程的localsum都会出错。我假设这是因为void*param和globalindex是由多个线程同时访问的。没有意义的是,尽管我将代码中的每个全局变量都设为mutexlock,但它还是发生了。 我该如何解决这个问题

#include<string>
#include<iostream>
#include<fstream>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

using namespace std;
int y =0;
int sum=0;
int array[1000000];
int x=0;
int leftoverHandle = 0;
int globalindex = 0;  
int eachThreadHandles =0;

void* add(void* param){
    pthread_mutex_lock(&mutex);
    int localindexup = globalindex + eachThreadHandles;
    int localindexdown = globalindex;
    int localsum=0;
    long localparam = (long)param;

    if(y != leftoverHandle ){
            localindexup++;
            y++;

    }
    pthread_mutex_unlock(&mutex);


    while(localindexdown<localindexup){

            pthread_mutex_lock(&mutex);
            sum = sum+array[localindexdown];
            localsum = localsum+array[localindexdown];
            localindexdown++;
            pthread_mutex_unlock(&mutex);


    }




    pthread_mutex_lock(&mutex);
    globalindex = localindexdown;
    printf("Thread %ld", localparam);
    printf(": %d\n", localsum);
    pthread_mutex_unlock(&mutex);
}

int main(int argc, char ** argv){
    if(argc != 3){
            cout<<"Incorrect number of argument";
            exit(1);
    }
    string line;
    string f = argv[1];
    const char *filename = f.c_str();



    int maxthreads = atoi(argv[2]);

    FILE* inFile = fopen(filename,"r");
    int i=0;
    if(inFile == NULL){
    cout<<"fopen failed"<<endl;
    }

    fscanf(inFile, "%d",&i);

    while(!feof(inFile)){

    array[x]=i;
    x +=1;

    fscanf(inFile,"%d",&i);

    }

    fclose(inFile);

    pthread_t id[maxthreads];

    leftoverHandle = x%maxthreads;
    eachThreadHandles = (x - leftoverHandle)/maxthreads;


    for(long i=0; i< maxthreads;i++){
            long status = pthread_create(&id[i], NULL, add, (void*) i);
            if(status){
                    printf("Error creating thread! \n");
                    exit(0);
            }
    }

    for(long i=0; i<maxthreads;i++){
    pthread_join(id[i], NULL);
    }


    cout<<"Sum="<<sum<<endl;




return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
使用名称空间std;
int y=0;
整数和=0;
整数数组[1000000];
int x=0;
int leftoverHandle=0;
int globalindex=0;
int-eachThreadHandles=0;
void*添加(void*参数){
pthread_mutex_lock(&mutex);
int localindexup=globalindex+eachThreadHandles;
int localindexdown=globalindex;
int localsum=0;
long localparam=(long)param;
如果(y!=左上手){
localindexup++;
y++;
}
pthread_mutex_unlock(&mutex);

而(localindexdown问题在于,在每个线程初始化
localindexup
localindexdown
之后,即在第一个关键部分,您没有立即更新
globalindex

代码中有三个关键部分。
假设
thread0
运行第一个关键部分,然后
thread1
thread0
释放第一个关键部分的锁后立即抢占
thread0
。但是因为您正在第三个关键部分而不是第一个关键部分中将
globalindex
设置为
localindexdown
,所以
thread1仍将看到
globalindex=0
,就像
thread0
,因此它将重新计算与
thread0
相同的总和。您应该将
globalindex=localindexdown;
放入第一个关键部分。
实际上根本不需要第三个临界段:

    pthread_mutex_lock(&mutex);
    int localindexup = globalindex + eachThreadHandles;
    int localindexdown = globalindex;
    int localsum=0;
    long localparam = (long)param;

    if(y != leftoverHandle ){
            localindexup++;
            y++;

    }
    globalindex = localindexdown; //<--- MOVED HERE
    pthread_mutex_unlock(&mutex);
pthread\u mutex\u lock(&mutex);
int localindexup=globalindex+eachThreadHandles;
int localindexdown=globalindex;
int localsum=0;
long localparam=(long)param;
如果(y!=左上手){
localindexup++;
y++;
}

globalindex=localindexdown;//不幸的是,它并没有解决我的问题。当我多次运行它时,仍然会出现争用条件。这就是为什么我对它如此困惑。您的代码将每个变量放入互斥锁中,但争用条件仍然存在。您能用线程调用显示代码吗(您实际使用的那些
add()
)?
    while(localindexdown<localindexup)
    {
            pthread_mutex_lock(&mutex);
            sum = sum+array[localindexdown];
            pthread_mutex_unlock(&mutex);     //<--- MOVED HERE
            localsum = localsum+array[localindexdown];
            localindexdown++;
    }