Linux C++ 使用condition实现阻塞队列

来源:互联网 发布:windows live影片制作 编辑:程序博客网 时间:2024/04/29 17:06
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * BlockingQueue.h 
  3.  * 
  4.  *  Created on: 2014年6月10日 
  5.  *      Author:  
  6.  */  
  7.   
  8. #ifndef BLOCKINGQUEUE_H_  
  9. #define BLOCKINGQUEUE_H_  
  10.   
  11. #include <iostream>  
  12. #include <pthread.h>  
  13.   
  14. using namespace std;  
  15.   
  16. //template <typename T >  
  17. class BlockingQueue  
  18. {  
  19. public:  
  20.     BlockingQueue();  
  21.     BlockingQueue(int capacity);  
  22.     ~BlockingQueue();  
  23.   
  24.     bool push(int item);  
  25.     int poll();  
  26.   
  27. private:  
  28.     int capacity;  
  29.     int* queue;  
  30.     int head,tail;  
  31.     pthread_mutex_t mutex;  
  32.     pthread_cond_t notFull,notEmpty;  
  33. };  
  34.   
  35.   
  36. #endif /* BLOCKINGQUEUE_H_ */  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * BlockingQueue.cpp 
  3.  * 
  4.  *  Created on: 2014年6月10日 
  5.  *      Author:  
  6.  */  
  7. #include "../include/BlockingQueue.h"  
  8.   
  9. BlockingQueue::BlockingQueue()  
  10. {  
  11.     this->capacity = 10;  
  12.     queue = new int[capacity];  
  13.     head = 0,tail = 0;  
  14.     pthread_mutex_init(&mutex,NULL);  
  15.     pthread_cond_init(&notFull,NULL);  
  16.     pthread_cond_init(&notEmpty,NULL);  
  17.   
  18. }  
  19.   
  20. BlockingQueue::BlockingQueue(int capacity)  
  21. {  
  22.     this->capacity = capacity;  
  23.     queue = new int[capacity];  
  24.     cout << "capacity " << sizeof(queue) << endl;  
  25.     head = 0,tail = 0;  
  26.     pthread_mutex_init(&mutex,NULL);  
  27.     pthread_cond_init(&notFull,NULL);  
  28.     pthread_cond_init(&notEmpty,NULL);  
  29.   
  30. }  
  31.   
  32. BlockingQueue::~BlockingQueue()  
  33. {  
  34.     this->capacity = 0;  
  35.     head = 0,tail = 0;  
  36.     delete queue;  
  37.     pthread_mutex_destroy(&mutex);  
  38.     pthread_cond_destroy(&notFull);  
  39.     pthread_cond_destroy(&notEmpty);  
  40. }  
  41.   
  42. bool BlockingQueue::push(int item)  
  43. {  
  44.     pthread_mutex_lock(&mutex);  
  45.     cout << "you want push " << item << endl;  
  46.     while((head + 1) % capacity == tail)//is full  
  47.     {  
  48.         cout << "is full,wait..." << endl;  
  49.         // push wait  
  50.         pthread_cond_wait(&notFull,&mutex);  
  51.         cout << "not full,unlock" << endl;  
  52.     }  
  53.   
  54.     {  
  55.         queue[head] = item;  
  56.         head = (head + 1) % capacity;  
  57.         cout << "push " << item << endl;  
  58.         //wake up poll thread  
  59.         pthread_cond_signal(&notEmpty);  
  60.         pthread_mutex_unlock(&mutex);  
  61.   
  62.         return true;  
  63.     }  
  64. }  
  65.   
  66. int BlockingQueue::poll()  
  67. {  
  68.     pthread_mutex_lock(&mutex);  
  69.     int ret = 0;  
  70.     while(head == tail) // is empty  
  71.     {  
  72.         cout << "is empty,wait..." << endl;  
  73.         //poll wait  
  74.         pthread_cond_wait(&notEmpty,&mutex);  
  75.         cout << "not empty,unlock..." << endl;  
  76.     }  
  77.     {  
  78.         ret = queue[tail];  
  79.         tail = (tail + 1) % capacity;  
  80.         cout << "take " << ret << endl;  
  81.         //wake up push thread  
  82.         pthread_cond_signal(&notFull);  
  83.   
  84.         pthread_mutex_unlock(&mutex);  
  85.         return ret;  
  86.     }  
  87. }  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "include/BlockingQueue.h"  
  3. using namespace std;  
  4. BlockingQueue queue(3);  
  5.   
  6. void* put(void *)  
  7. {  
  8.     queue.push(1);  
  9.         queue.push(2);  
  10.         queue.push(3);  
  11.         queue.push(4);  
  12.         queue.push(5);  
  13.         return NULL;  
  14. }  
  15.   
  16. void* take(void *)  
  17. {  
  18.     queue.poll();  
  19.     queue.poll();  
  20.     queue.poll();  
  21.     return NULL;  
  22. }  
  23.   
  24.   
  25. int main() {  
  26.   
  27.     pthread_t put1,take1;  
  28.     pthread_create(&put1,NULL,put,0);  
  29.     pthread_create(&take1,NULL,take,0);  
  30.   
  31.     void * retval;  
  32.     pthread_join(put1,&retval);  
  33.     pthread_join(take1,&retval);  
  34.   
  35.     return 0;  
  36. }  

0 0