The simplest producer consumer-pthread

Source: Internet
Author: User

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:
    1. Unlocks a lock that has been locked by the calling thread
    2. Wait condition, sleep jam
    3. Conditions come and wake up
    4. 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 (&AMP;PC_CONDP, &pc_mutex); }//ProductionBuf. Num= i; printf"producer produces%d \ n", buf. Num);//NotificationPthread_cond_signal (&AMP;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 (&AMP;PC_CONDC, &pc_mutex); }//conditions reachedprintf"consumer consumes%d \ n", buf. Num); Buf. Num=0;//Notify consumerPthread_cond_signal (&AMP;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 (&AMP;PC_CONDP,NULL); Pthread_cond_init (&AMP;PC_CONDC,NULL);    Pthread_attr_init (&AMP;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 (&AMP;PC_CONDC);    Pthread_cond_destroy (&AMP;PC_CONDP);    Pthread_attr_destroy (&AMP;ATTR); Pthread_exit (NULL);return 0;}

[1] http://blog.chinaunix.net/uid-27164517-id-3282242.html
*/

Simplest producer Consumer-pthread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.