Conditional Variable Message Queue and Conditional Variable Message Queue

Source: Internet
Author: User

Conditional Variable Message Queue and Conditional Variable Message Queue

Conditional variables are another mechanism for synchronization before the thread. Conditional variables provide a place for multithreading. When condition variables are used together with mutex locks, threads are allowed to wait for specific conditions in a non-competitive manner. This greatly reduces the thread scheduling and thread waiting caused by lock competition.

Message Queue is a hurdle that cannot be bypassed during server development. Previously, I have implemented a Message Queue Based on mutex lock and three queues, and the performance is very good. Other owners in the blog community have also implemented many message queues Based on the ring queue and lock-free, which are very good, today, we will implement a Message Queue Based on Double Buffering, mutex lock, and conditional variables. For more information, see blockingqueue in java !! Although the queue based on the three buffers excludes thread competition to the maximum extent, there are very few players. When messages are very small, some buckets need to be added to fill the data. This is probably a defect!

What are the main objects used by message queues during server development?

1: I think it is probably the interaction between the communication layer and the logic layer. The network data received by the communication layer is passed to the logic layer through the Message Queue after the packets are verified, the logic layer transfers the processing result packet to the communication layer!

2: separation of logical threads and database I/O threads; database I/O threads are responsible for database read/write updates; logical layer operations on databases are encapsulated into messages to request database I/O threads, after the database IO thread is processed, it is handed back to the logic layer.

3: log. The processing mode is similar to method 2. However, logs do not need to be returned!

Source code:

BlockingQueue. h file

/* * BlockingQueue.h * *  Created on: Apr 19, 2013 *      Author: archy_yu */ #ifndef BLOCKINGQUEUE_H_#define BLOCKINGQUEUE_H_ #include <queue>#include <pthread.h> typedef void* CommonItem; class BlockingQueue{public:    BlockingQueue();     virtual ~BlockingQueue();     int peek(CommonItem &item);     int append(CommonItem item); private:     pthread_mutex_t _mutex;     pthread_cond_t _cond;     std::queue<CommonItem> _read_queue;     std::queue<CommonItem> _write_queue; };#endif /* BLOCKINGQUEUE_H_ */

BlockingQueue. cpp File Code

/* * BlockingQueue.cpp * *  Created on: Apr 19, 2013 *      Author: archy_yu */ #include "BlockingQueue.h" BlockingQueue::BlockingQueue(){    pthread_mutex_init(&this->_mutex,NULL);    pthread_cond_init(&this->_cond,NULL);} BlockingQueue::~BlockingQueue(){    pthread_mutex_destroy(&this->_mutex);    pthread_cond_destroy(&this->_cond);} int BlockingQueue::peek(CommonItem &item){     if( !this->_read_queue.empty() )    {        item = this->_read_queue.front();        this->_read_queue.pop();    }    else    {        pthread_mutex_lock(&this->_mutex);         while(this->_write_queue.empty())        {            pthread_cond_wait(&this->_cond,&this->_mutex);        }         while(!this->_write_queue.empty())        {            this->_read_queue.push(this->_write_queue.front());            this->_write_queue.pop();        }         pthread_mutex_unlock(&this->_mutex);    }    return 0;} int BlockingQueue::append(CommonItem item){    pthread_mutex_lock(&this->_mutex);    this->_write_queue.push(item);    pthread_cond_signal(&this->_cond);    pthread_mutex_unlock(&this->_mutex);    return 0;}

Test code:

BlockingQueue _queue; void* process(void* arg){     int i=0;    while(true)    {        int *j = new int();        *j = i;        _queue.append((void *)j);        i ++;    }    return NULL;} int main(int argc,char** argv){    pthread_t pid;    pthread_create(&pid,0,process,0);     long long int start = get_os_system_time();    int i = 0;    while(true)    {        int* j = NULL;        _queue.peek((void* &)j);         i ++;         if(j != NULL && (*j) == 100000)        {            long long int end = get_os_system_time();            printf("consume %d\n",end - start);            break;        }    }     return 0;}

Welcome !!!

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.