Thinking and finishing of basic knowledge
http://blog.csdn.net/aganlengzi/article/details/51345294
The most basic producer consumer model:
A producer
A consumer
A buffer
A lock
Two condition variables
/*
Approximate operating procedures for pthread_cond_wait:
- Unlocks a lock that has been locked by the calling thread
- Wait condition, sleep jam
- Conditions come and wake up
- Lock unlocked by locking the unlocked calling thread before returning
Pthread_cond_signal is used to wake up a thread waiting on a condition variable, typically 1 pthread_cond_broadcast wake up all the threads waiting on a certain condition variable why are you using the mutex lock binding?
The purppose of lock is to prevent simultaneous (simultaneous) request of Wait ()
If cond_signal happens, it'll stop blocking and lock the mutex atomatically.
Location of signal and lock
Pthread_cond_signal can be placed between Pthread_mutex_lock and Pthread_mutex_unlock,
It can also be placed after Pthread_mutex_lock and Pthread_mutex_unlock, but each has its drawbacks.
Between:
Pthread_mutex_lock
xxxxxxx
Pthread_cond_signal
Pthread_mutex_unlock
Pros: The downside of the following way
Disadvantage: In some thread implementations, the wait thread (the thread that calls wait) wakes up from the kernel (due to cond_signal) and then back to the kernel space (since cond_wait returns with atomic locking behavior), so one time there is a performance problem.
However, there is no problem with Linux threads because there are two queues in the Linux thread, namely the cond_wait queue and the Mutex_lock queue,
Cond_signal just lets threads move from the cond_wait queue to the Mutex_lock queue without having to return to the user space without the loss of performance.
So it is recommended to use this mode in Linux.
After:
Pthread_mutex_lock
xxxxxxx
Pthread_mutex_unlock
Pthread_cond_signal
Pros: The disadvantage of the above way
Cons: If unlock and signal have a low-priority thread (others) waiting on the mutex,
Then this low-priority thread will preempt the high-priority thread (assuming that the wait is the cond thread), because the resource gets executed,
And wait this cond thread resource has not got, can only wait. A typical priority rollover.
/* Producer-Consumer basic Model a producer one consumer a buffer needs: 1. A lock of buffer 2. Buffer 3. Notification mechanism, producer consumer 4. Flag producers and consumers Between the two Protocols 5. A producer thread 6. A consumer thread * /#include <pthread.h>#include <stdio.h>//buffer data types can be extendedtypedef struct{intNum;} BUFFER;#define Max_num//bufferBUFFER buf;//lockpthread_mutex_t Pc_mutex;//Notificationpthread_cond_t PC_CONDP, PC_CONDC;/ * producer * /void* Producer (void* nul) {intI for(i =1; i < max_num; ++i) {pthread_mutex_lock (&pc_mutex);//Wait condition variable while(buf. Num!=0) {pthread_cond_wait (&PC_CONDP, &pc_mutex); }//ProductionBuf. Num= i; printf"producer produces%d \ n", buf. Num);//NotificationPthread_cond_signal (&PC_CONDC); Pthread_mutex_unlock (&pc_mutex); } pthread_exit (NULL);}/ * Consumer * /void* Consumer (void* nul) {intI for(i =1; i < max_num; ++i) {pthread_mutex_lock (&pc_mutex);//Wait condition variable while(buf. Num==0) {pthread_cond_wait (&PC_CONDC, &pc_mutex); }//conditions reachedprintf"consumer consumes%d \ n", buf. Num); Buf. Num=0;//Notify consumerPthread_cond_signal (&PC_CONDP); Pthread_mutex_unlock (&pc_mutex); } pthread_exit (NULL);}intMainintargcChar Const*argv[]) {pthread_t thread[2]; pthread_attr_t attr; Buf. Num=0;//Locks and condition variablesPthread_mutex_init (&pc_mutex,NULL); Pthread_cond_init (&PC_CONDP,NULL); Pthread_cond_init (&PC_CONDC,NULL); Pthread_attr_init (&ATTR); Pthread_attr_setdetachstate (&attr, pthread_create_joinable);//producerPthread_create (&thread[0], &attr, producer,NULL);//consumerPthread_create (&thread[1], &attr, consumer,NULL);//Connection threadPthread_join (thread[0],NULL); Pthread_join (thread[1],NULL);//Cleanup ResourcesPthread_mutex_destroy (&pc_mutex); Pthread_cond_destroy (&PC_CONDC); Pthread_cond_destroy (&PC_CONDP); Pthread_attr_destroy (&ATTR); Pthread_exit (NULL);return 0;}
[1] http://blog.chinaunix.net/uid-27164517-id-3282242.html
*/
Simplest producer Consumer-pthread