C + + blocking queue

Source: Internet
Author: User

A blocking queue is a basic data structure for multithreaded asynchronous architectures in background development, such as Python, where Java provides a thread-safe blocking queue, and C + + may need to implement a template on its own.

From performance considerations, you do not use the STL queue as the basic data structure, but use a circular array as the basic data structure, performance should be higher than the queue, eliminating the dynamic memory allocation and recycling.

To be sure, the queue size is not dynamically scalable, and in real-world development, you can configure the appropriate queue size by using stress tests.


/********************************************function:thread Safe Blocking Queue.author:liuyidate: 2014.11.13version:2.0********************************************/#ifndef block_queue_h#define BLOCK_QUEUE_H# Include <iostream> #include <pthread.h> #include <sys/time.h>using namespace Std;template<class T >class block_queue{public:block_queue (int max_size = +) {if (max_size <= 0) {exit (-1);}  M_max_size = Max_size;m_array = new T[max_size];m_size = 0;m_front = -1;m_back = -1;m_mutex = new Pthread_mutex_t;m_cond = New Pthread_cond_t;pthread_mutex_init (M_mutex, NULL);p thread_cond_init (m_cond, null);} ~block_queue () {pthread_mutex_lock (M_mutex); if (m_array! = NULL) Delete m_array;pthread_mutex_unlock (M_mutex); Pthread_mutex_destroy (M_mutex);p Thread_cond_destroy (m_cond);d elete m_mutex;delete M_cond;} BOOL Full () Const{pthread_mutex_lock (M_mutex); if (m_size >= m_max_size) {pthread_mutex_unlock (M_mutex); return true ;} Pthread_mutex_unlock (M_mutex); return false;} bool Empty () COnst{pthread_mutex_lock (M_mutex); if (0 = = m_size) {pthread_mutex_unlock (M_mutex); return true;} Pthread_mutex_unlock (M_mutex); return false;} T Front () const{t tmp;pthread_mutex_lock (M_mutex); tmp = M_array[m_front];p thread_mutex_unlock (M_MUTEX); return tmp;} T back () const{t tmp;pthread_mutex_lock (M_mutex); tmp = M_array[m_back];p thread_mutex_unlock (M_MUTEX); return tmp;} int size () const{int tmp = 0;pthread_mutex_lock (M_MUTEX); tmp = M_size;pthread_mutex_unlock (M_MUTEX); return tmp;} int max_size () const{int tmp = 0;pthread_mutex_lock (M_MUTEX); tmp = M_max_size;pthread_mutex_unlock (M_MUTEX); return tmp ;} BOOL Push (const t& Item) {Pthread_mutex_lock (M_mutex); if (m_size >= m_max_size) {pthread_cond_broadcast (M_cond) ;p Thread_mutex_unlock (M_mutex); return false;} M_back = (m_back + 1)% m_max_size;m_array[m_back] = item;m_size++;p thread_cond_broadcast (m_cond);p Thread_mutex_unlock (M_mutex); return true;} BOOL Pop (t& Item) {Pthread_mutex_lock (M_mutex); while (m_size <= 0) {if (0! = pthread_cond_wait (M_cond, M_mutex) {pthread_mutex_unlock (M_mutex); return false;}} M_front = (m_front + 1)% M_max_size;item = m_array[m_front];m_size--;p thread_mutex_unlock (M_mutex); return true;} BOOL Pop (t& item, int ms_timeout) {struct Timespec t = {0,0};struct Timeval now = {0,0};gettimeofday (&now, NULL);p t Hread_mutex_lock (M_mutex); if (m_size <= 0) {t.tv_sec = now.tv_sec + ms_timeout/1000;t.tv_nsec = (ms_timeout% 1000) * 1000;if (0! = pthread_cond_timedwait (M_cond, M_mutex, &t)) {Pthread_mutex_unlock (M_mutex); return false;}} M_front = (m_front + 1)% M_max_size;item = m_array[m_front];m_size--;p thread_mutex_unlock (M_mutex); return true;} private:pthread_mutex_t *m_mutex;pthread_cond_t *m_cond; T *m_array;int m_size;int m_max_size;int m_front;int m_back;}; #endif

Test program

#include <iostream> #include "block_queue.h" using namespace std;block_queue<int> g_queue (+); void *p ( void *args) {sleep (1), int data = 0;for (int i = 0; i <; i++) {G_queue.push (data++);} return NULL;} void *c (void* args) {while (true) {int t = 0;if (!g_queue.pop (t,1000)) {cout<< "Timeout" <<endl;continue;} Else{cout<<t<<endl;} G_queue.pop (t);cout<< "block=" <<T<<ENDL;} return NULL;} int main () {pthread_t id;pthread_create (&id, NULL, p, NULL);//pthread_create (&id, NULL, p, NULL);//pthread_ Create (&id, NULL, C, NULL);p thread_create (&id, NULL, C, NULL); for (;;) Sleep (1); return 0;}


C + + blocking queue

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.