c++ 安全執行緒阻塞隊列,線程阻塞隊列

來源:互聯網
上載者:User

c++ 安全執行緒阻塞隊列,線程阻塞隊列

阻塞隊列是後台開發中多線程非同步架構的基本資料結構,像python, java 都提供安全執行緒的阻塞隊列,c++ 可能需要自己實現一個模板。

從效能考慮,自己沒有使用STL的queue作為基本資料結構,而是使用迴圈數組作為基本資料結構,效能應該比queue高,省去了動態記憶體分配和回收。

確定就是,隊列大小不可動態擴充,當時實際開發中,可以通過壓力測試,配置合適的隊列大小。


/********************************************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 = 1000){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);pthread_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);pthread_cond_destroy(m_cond);delete 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;}bool front(T& value)const{pthread_mutex_lock(m_mutex);if(0 == m_size){pthread_mutex_unlock(m_mutex);return false;}value = m_array[m_front];pthread_mutex_unlock(m_mutex);return true;}bool back(T& value)const{pthread_mutex_lock(m_mutex);if(0 == m_size){pthread_mutex_unlock(m_mutex);return false;}value = m_array[m_back];pthread_mutex_unlock(m_mutex);return true;}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);pthread_mutex_unlock(m_mutex);return false;}m_back = (m_back + 1) % m_max_size;m_array[m_back] = item;m_size++;pthread_cond_broadcast(m_cond);pthread_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--;pthread_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);pthread_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--;pthread_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


//測試程式

#include<iostream>#include"block_queue.h"using namespace std;block_queue<int> g_queue(100);void *p(void *args){sleep(1);int data = 0;for(int i = 0; i < 100; 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);pthread_create(&id, NULL, c, NULL);for(;;)sleep(1);return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.