2013-02-25 49 views
2

我正在使用互斥试图限制对某个线程的代码的某些部分的访问,但而不是锁定一次,并阻止其他线程,它似乎允许所有线程“锁定”。以下是我的代码,然后显示代码不工作的输出部分。PThread互斥不作为希望

//headers defined, etc 

pthread_mutex_t queuemutex = PTHREAD_MUTEX_INITIALIZER; 

// other code with various functions 

int main(void) { 

    //unrelated code 

    threadinformation **threadArray = (threadinformation **)malloc(POOLSIZE * sizeof(threadinformation)); 

    int k; 
    for (k = 0; k < POOLSIZE; k++) { 
     pthread_t thread; 
     threadinformation *currentThread = (threadinformation *)malloc(sizeof(threadinformation)); 
     currentThread->state = (int *)malloc(sizeof(int)); 
     currentThread->state[0] = 0; 
     currentThread->currentWaiting = currentWaiting; 
     currentThread->number = k; 
     threadArray[k] = currentThread; 
     pthread_create(&thread, NULL, readWriteToClient, threadArray[k]); //thread is created here 
     currentThread->thread = thread; 
     joinArray[k] = thread; 
    } 

    //unrelated code 

} 

static void* readWriteToClient(void *inputcontent) { 

    while(1){ 

     //unrelated code 

     pthread_mutex_lock(&queuemutex); //problem happens here 

     fprintf(stderr,"Thread %d got locked \n",threadInput->number); 

     while((threadInput->currentWaiting->status) == 0){ 
      pthread_cond_wait(&cond, &queuemutex); 
      fprintf(stderr,"Thread %d got signalled \n",threadInput->number); 
     } 

     connfd = threadInput->currentWaiting->fd; 
     threadInput->currentWaiting->status = 0; 
     pthread_cond_signal(&conncond); 
     pthread_mutex_unlock(&queuemutex); 

     //unrelated code 

    } 

} 

输出。

Thread 0 got locked 
Thread 7 got locked 
Thread 25 got locked 
Thread 97 got locked 
Thread 6 got locked 
Thread 5 got locked 
Thread 4 got locked 
Thread 3 got locked 
Thread 8 got locked 
Thread 9 got locked 
Thread 10 got locked 
Thread 11 got locked 
Thread 12 got locked 
Thread 13 got locked 
Thread 14 got locked 
Thread 15 got locked 
Thread 16 got locked 
Thread 17 got locked 
Thread 18 got locked 
Thread 19 got locked 
Thread 20 got locked 
    And so on... 
+0

Threadinput-> currentwaiting-> status声明为volatile吗? – 2013-02-26 00:50:20

+0

你应该考虑删除那个问题,现在对方得到了答案! – didierc 2013-02-26 13:21:37

回答

3

没有问题。

pthread_cond_wait(& cond,& queuemutex);

等待条件变量释放互斥量。

+0

同样重要的是,它在pthread_cond_wait返回时重新使用它 – nos 2013-12-12 21:26:07

1

什么fceller说更长的版本是,调用pthread_cond_wait(& COND,&互斥)做了三件事在返回之前:它释放锁,然后等待一个信号,然后等待(如有必要)重新获得锁定。

您的示例没有显示发送第一个信号的内容(如果有的话),并且不会显示工作线程正在等待的状态(如果有的话)。

这是pthread_cond_wait()和pthread_cond_signal()的更典型用例。

void producer() { 
    pthread_mutex_lock(&mutex); 
    push_something_on_the_queue(); 
    ptherad_cond_signal(&cond); 
    pthread_mutex_unlock(&mutex); 
} 

void consumer() { 
    pthread_mutex_lock(&mutex); 
    while (! time_to_quit) { 
     if (queue_is_empty) { 
      pthread_cond_wait(&cond, &mutex); 
     } else { 
      thing = take_something_from_the_queue(); 

      pthread_mutex_unlock(&mutex); 
      do_something_with(thing); 
      pthread_mutex_lock(&mutex); 
     } 
    } 
    pthread_mutex_unlock(&mutex); 
} 

生产者线程将东西放入队列中。消费者线程等待事件出现在队列中,然后将它们弹出并对它们做些什么。消费者在一个循环中等待,每次唤醒时它都会检查队列的状态(即使队列中没有任何东西,线程可能会“虚假地”发出信号)。

当消费者正在检查队列时,互斥锁被锁定,并且它将保持锁定状态,直到消费者休眠或将某事从队列中弹出。当消费者正在睡觉时,互斥锁是而不是,当消费者正在做某件事时,它不会被锁定。

任何数量的消费者可以同时在consumer()方法中。他们可能在不同的事情上工作,或者他们可能在睡觉;但是互斥体确保了在任何给定时间只有一个线程(生产者或消费者)可以触摸队列。