Linux C + + is a simple producer consumer-line model

Source: Internet
Author: User

Introduction

Producer Consumer is a classic model

The coupling between the producer and the consumer is reduced by the producer, consumer and buffer

Changes to the producer and consumer

The following is a model of a typical life-style consumer.

Design ideas

In the team as a buffer zone, the FIFO of the product

The producer uses the push function of the buffer zone to add the product to the buffer zone.

Consumers adjust the pop function in the buffer zone to remove the product from the buffer zone.

Because the producer and consumer are different threads, so set the lock

The voice of the human being

Class cachequeue{    private:        /**         * @brief buffer row */        queue<int>* _requests;        /**         * @brief Mutex lock         **/        pthread_mutex_t _mutex;        /**         * @brief Queue not full conditional object         **/        pthread_cond_t _not_full_cond;        /**         * @brief Queue not empty conditional object         **/        pthread_cond_t _not_empty_cond;        uint32_t _bufsize;            Public:            chachequeue ();        void Setmaxlength (uint32_t bufSize);        /**          * @brief Add product to Team column          * @param [in] req: Product          **/        void Push (int req) to be added;        /**          * @brief remove a product from the squad          * @param [Return]: the product taken from the team column          **/        int Pop (uint32_t timeout);        /**          * @brief structure function          **/        ~cachequeue ();};

Important functions are push and pop, the producer uses push to add products to the buffer area, and the consumer uses the POP function to obtain the product.

Thread condition _not_full_cond indicates that the team is not satisfied, can add product

The thread condition _not_empty_cond indicates that the team is not empty and can obtain product

Push function

void Cachequeue::P ush (int req) {/*** lock */    Pthread_mutex_lock (&_mutex);/*** if the queue is full, wait for the letter *    /while (_ Requests->size () = = _bufsize)    {        pthread_cond_wait (&_not_full_cond, &_mutex);    }    _requests->push (req);/*** send non-empty letter *    /pthread_cond_signal (&_not_empty_cond);/*** unlock */    pthread_mutex_ Unlock (&_mutex);}

Pop function

int Cachequeue::P op (uint32_t timeout) {    int ret = 0;    int req = no_data;/*** lock *    /Pthread_mutex_lock (&_mutex);/*** queue empty wait for specified time */    struct timeval now;struct Timespec Timepass;gettimeofday (&now, NULL); timepass.tv_sec = now.tv_sec + timeout;timepass.tv_nsec = 0;    while (ret = = 0 && _requests->empty ())    {ret = pthread_cond_timedwait (&_not_empty_cond, &_mutex, &timepass);    } /*** There are no figures, and no numbers are returned    (ret!=0)    {        pthread_mutex_unlock (&_mutex);        return req;    } /*** returns the numbers, sending the team a non-full letter    /req = _requests->front ();    _requests->pop ();    Pthread_cond_signal (&_not_full_cond);/*** unlock *    /Pthread_mutex_unlock (&_mutex);    return req;}

Linux C + + is a simple producer consumer-line model

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.