2013-09-28 4 views
1
синхронизация

Я новичок в Pthreads и я написал пример программы, чтобы использовать условные события в PTHREAD ..Pthread запрос

Резьба2 просто оленья кожа выход ... Намерение было бы довольно ясно из кода я думаю.

#include<stdio.h> 
#include<stdlib.h> 
#include<pthread.h> 

pthread_mutex_t mut; 
pthread_cond_t con; 

#define LOCK &mut 
#define COND &con 

#define HAPPY 1 
#define SAD 0 

int count = 0, response = SAD; 

void func_t1(); 
void func_t2(); 

int main() 
{ 
    pthread_t tid1, tid2; 

    pthread_create(&tid1, NULL, &func_t1, NULL); 
    pthread_create(&tid2, NULL, &func_t2, NULL); 

    pthread_join(tid1, NULL); 
    pthread_join(tid2, NULL); 

    return 0; 
} 

void func_t1() 
{ 
    for (;;) 
    { 
    pthread_mutex_lock(LOCK); 

    pthread_cond_wait(COND, LOCK); 
    if (count == 10) 
    { 
     printf("Thread1: You did your job finally, get lost now"); 
     response = HAPPY; 
     pthread_mutex_unlock(LOCK); 
     break; 
    } 
    else 
    { 
     printf("Thread1: You are not capable of making me happy"); 

     pthread_mutex_unlock(LOCK); 
    } 
    } 
} 

void func_t2() 
{ 
    for (;;) 
    { 
    if (response == SAD) 
    { 
     pthread_mutex_lock(LOCK); 
     count++; 
     printf("thread2: count incremented to %d...pls check if you are happy \n", 
      count); 
     pthread_cond_signal(COND); 
     pthread_mutex_unlock(LOCK); 
    } 
    else 
    { 
     printf("thread2:Ha..I finally made her happy \n"); 
     break; 
    } 
    } 
} 

Выход:

thread2: count incremented to 106927...pls check if you are happy 
thread2: count incremented to 106928...pls check if you are happy 
thread2: count incremented to 106929...pls check if you are happy 
thread2: count incremented to 106930...pls check if you are happy 
thread2: count incremented to 106931...pls check if you are happy 
thread2: count incremented to 106932...pls check if you are happy 
thread2: count incremented to 106933...pls check if you are happy 
thread2: count incremented to 106934...pls check if you are happy 
thread2: count incremented to 106935...pls check if you are happy 
thread2: count incremented to 106936...pls check if you are happy 
thread2: count incremented to 106937...pls check if you are happy 
thread2: count incremented to 106938...pls check if you are happy 
thread2: count incremented to 106939...pls check if you are happy 
thread2: count incremented to 106940...pls check if you are happy 
thread2: count incremented to 106941...pls check if you are happy 
thread2: count incremented to 106942...pls check if you are happy 
thread2: count incremented to 106943...pls check if you are happy 
Thread1: You are not capable of making me happythread2: count incremented to 106944...pls check if you are happy 
thread2: count incremented to 106945...pls check if you are happy 
thread2: count incremented to 106946...pls check if you are happy 
thread2: count incremented to 106947...pls check if you are happy 
thread2: count incremented to 106948...pls check if you are happy 
thread2: count incremented to 106949...pls check if you are happy 
thread2: count incremented to 106950...pls check if you are happy 
thread2: count incremented to 106951...pls check if you are happy 
thread2: count incremented to 106952...pls check if you are happy 
thread2: count incremented to 106953...pls check if you are happy 
thread2: count incremented to 106954...pls check if you are happy 
thread2: count incremented to 106955...pls check if you are happy 
thread2: count incremented to 106956...pls check if you are happy 
thread2: count incremented to 106957...pls check if you are happy 
thread2: count incremented to 106958...pls check if you are happy 
thread2: count incremented to 106959...pls check if you are happy 

Это никогда не закончится ....

Было бы полезно, если вы можете указать на недостатки в коде выше.

ответ

0

Таким образом, вы в основном имеют программу с двумя потоками:

Первый поток ждет графа будет приходить 10. Он просыпается по сигналу от нити 2.

резьбы 2 увеличивает переменную счетчика и посылает сигнал, и повторяет.

Там нет ничего, что гарантирует, что резьба 1 будет видеть все изменения сосчитать, так как резьба 2 не ждать ответа от Thread 1.

Вместо этого она может выглядеть следующим образом:

Thread 2 increments count & signals 
Thread 2 increments count & signals 
Thread 2 increments count & signals 
Thread 2 increments count & signals 
Thread 1 sees signal - not 10 
Thread 2 increments count & signals 
Thread 2 increments count & signals 
Thread 2 increments count & signals 
Thread 1 sees signal - not 10 
... 
... 

Вы необходимо добавить механизм, который позволяет только Thread 2 увеличивать счетчик, если Thread 1 проверяет текущее значение count и является SAD.