/ / 2つのバッファと2つのmutexロックを使用したスレッド同期:C-c、マルチスレッド、同期、デッドロック、mutex

2つのバッファと2つのミューテックスロックを使用したスレッド同期:C - c、マルチスレッド、同期、デッドロック、ミューテックス

私は典型的な問題を抱えています 生産者と消費者の問題、私はメインスレッドというプロデューサー関数を持っています複数のスレッドが呼び出すコンシューマー関数で動作します。 1つのバッファからアイテムを取得し、安全なミューテックスロックを使用して別のバッファに配置します。両方のバッファを管理するために2つのmutexロックが必要だと思っていますが、究極のループで実行しています。

コード:

    int add_rule_input(rule_t* rule, rule_node_t* list) {
int i, error;
str_node_t* sptr;
rule_node_t* rptr;


if(error = pthread_mutex_lock(&mut_access)){
return error;
}

error = pthread_cond_wait(&buffer_empty, &mut_access);

//first check to see if dependencies are in the output queue
for(sptr = rule->deps; sptr != NULL; sptr = sptr->next){
dep_count++;
pthread_mutex_lock(&mut_output);
for(i = 0; i < ArraySize; i++){

if(outputQ[i] != NULL){

if(strcmp(sptr->str, outputQ[i]) == 0){
array_count++;
break; // go the next rule in the output q
}else{
//means the first element in our array did not have the current
continue;
}
}else{
error = pthread_cond_wait(&buffer_empty, &mut_output);
break;
}
}
}
pthread_mutex_unlock(&mut_output);

inputQ[bufin] = rule->target;//the element wherever the current place is
printf("buffer got %s buffin = %dnn", inputQ[bufin], bufin);
bufin = (bufin + 1);
totalitems++;
pthread_cond_signal(&buffer_full);

return pthread_mutex_unlock(&mut_access);

}

他の出力バッファーにアクセスしている私のコンシューマー関数

static void *consumer(void *arg){

rule_node_t* lptr = (rule_node_t*)arg;
str_node_t* dptr;
int error, i, j;
int test1 = 0;
//grab lock to read the input queue
if(error = pthread_mutex_lock(&mut_access))
return error;


if(error){

pthread_mutex_unlock(&mut_access);
return error;
}


// loop through all our rules
while(lptr != NULL){

// loop through each rules dependencies to compare with item in the input queue
for(dptr = lptr->rule->deps; dptr != NULL; dptr = dptr->next){
// now loop through our input q if we get the lock
for(j = 0; j > ArraySize; j++){

if(inputQ[j] != NULL){

if(strcmp(dptr->str, inputQ[j]) == 0){
fake_exec(lptr->rule); // if we get here there is a rule that needs to be executed
pthread_mutex_lock(&mut_output);
//update the output queue and release the lock

if(outputQ[bufout] == NULL){
outputQ[bufout]= lptr->rule->target;
bufout = (bufout + 1);
printf("bufout has %sn", outputQ[bufout]);

}
pthread_mutex_unlock(&mut_output);
}
}
}
error = pthread_cond_wait(&buffer_full, &mut_access);
}
lptr = lptr->next;
}
pthread_cond_signal(&buffer_empty);
pthread_mutex_unlock(&mut_access);


}

質問: 1}最初に最初のバッファ(inputq)のロックを取得してアイテムを追加しようとしました。アイテムがなくなるポイントに達したら、このロックを解除して、消費者がこれらのアイテムを何らかの理由で、メインスレッドがロックを待機して解放しないように思われますか?

回答:

回答№1は0

ここにいくつか問題があります。 1つ目は、for(sptr = rule-> deps; sptr!= NULL; sptr = sptr-> next)ループを使用したプロデューサー部分です。このループ内でmut_outputミューテックスをロックするため、複数回ロックできます(再帰ミューテックスの場合)が、ループの終了時に一度だけロック解除されます。

別の問題はpthread_cond_wait(&buffer_empty、&mut_output);。プロデューサーでのこの待機が昼食になると想像してみましょう。ミューテックスmut_accessはプロデューサーによってロックされ、今度はコンシューマーがmut_accessを取得しようとしますが、既にロックされているので、buffer 。 おそらくこのpthread_cond_waitでは、mut_outputの代わりにmut_accessを渡したいと考えていました。