简体   繁体   English

使用两个缓冲区和两个互斥锁的线程同步:C

[英]Thread Synchronization using two buffers and two mutex locks: C

I am having trouble with the typical Producer & Consumer problem , i have a producer function that the main thread will be operating with and a consumer function that multiple threads call; 我遇到了典型的生产者和消费者问题 ,我有一个主线程将要使用的生产者函数和一个多个线程调用的消费者函数; taking items from one buffer and placing them in another using a safe mutex lock. 从一个缓冲区中取出项目,然后使用安全的互斥锁将它们放在另一个缓冲区中。 Im thinking i need two mutex locks to manage both buffers but i am running in an ultimate loop. 我以为我需要两个互斥锁来管理两个缓冲区,但我正在一个最终循环中运行。

CODE: 码:

    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 = %d\n\n", inputQ[bufin], bufin);
bufin = (bufin + 1);
totalitems++; 
pthread_cond_signal(&buffer_full);

return pthread_mutex_unlock(&mut_access);

}

My consumer function that is accessing the other output buffer 我的使用者函数正在访问另一个输出缓冲区

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 %s\n", 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);  


 }

QUESTIONS: 1} I have tried to first grab the lock for my first buffer(inputq) and add items to it, if i reach a point where there are no more items then i want to release this lock so my consumer can take these items from the buffer and place them in the outputq, for some reason it seems as the main thread does not wait and release the lock? 问题:1}我试图首先抓住我的第一个缓冲区(inputq)的锁并向其中添加项目,如果我到达一个没有更多项目的地步,那么我想释放此锁,以便我的消费者可以拿走这些项目从缓冲区并将它们放置在outputq中,由于某种原因,似乎主线程不等待并释放锁?

I see here few problems. 我在这里看到一些问题。 First one is in producer part with for(sptr = rule->deps; sptr != NULL; sptr = sptr->next) loop. 第一个是在生产器部分中的for(sptr = rule-> deps; sptr!= NULL; sptr = sptr-> next)循环。 Inside this loop you lock mut_output mutex, so it can be locked multiple time (if it is recursive mutex), but is unlocked only once when loop ends. 在此循环内,您将锁定mut_output互斥锁,因此可以多次锁定(如果它是递归互斥锁),但是在循环结束时只能被解锁一次。

Another problem is with pthread_cond_wait(&buffer_empty, &mut_output);. 另一个问题是pthread_cond_wait(&buffer_empty,&mut_output);。 Lets imagine this wait in producer is lunched. 让我们想象这个在生产者中的等待午餐。 When it is lunched mutex mut_access is locked by producer, and now when consumer is executed it try to acquire mut_access, but it cant because it it already locked so the consumer waits as well and newer reach part when it signal buffer_empty conditional variable to unlock producer. 午餐时,互斥锁mut_access被生产者锁定,现在执行消费者时,它会尝试获取mut_access,但由于已被锁定而无法进行操作,因此消费者在等待buffer_empty条件变量来解锁生产者时也将等待并获得更新。 Probably in this pthread_cond_wait you wanted to pass mut_access instead of mut_output. 可能在此pthread_cond_wait中,您想传递mut_access而不是mut_output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM