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

 更新时间:2017年01月06日 10:04:49   投稿:jingxian  
下面小编就为大家带来一篇Linux C++ 使用condition实现阻塞队列的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

/*
 * BlockingQueue.h
 *
 * Created on: 2014年6月10日
 *   Author: 
 */

#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_

#include <iostream>
#include <pthread.h>

using namespace std;

//template <typename T >
class BlockingQueue
{
public:
	BlockingQueue();
	BlockingQueue(int capacity);
	~BlockingQueue();

	bool push(int item);
	int poll();

private:
	int capacity;
	int* queue;
	int head,tail;
	pthread_mutex_t mutex;
	pthread_cond_t notFull,notEmpty;
};


#endif /* BLOCKINGQUEUE_H_ */
/*
 * BlockingQueue.cpp
 *
 *  Created on: 2014年6月10日
 *      Author: 
 */
#include "../include/BlockingQueue.h"

BlockingQueue::BlockingQueue()
{
    this->capacity = 10;
    queue = new int[capacity];
    head = 0,tail = 0;
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&notFull,NULL);
    pthread_cond_init(&notEmpty,NULL);

}

BlockingQueue::BlockingQueue(int capacity)
{
    this->capacity = capacity;
    queue = new int[capacity];
    cout << "capacity " << sizeof(queue) << endl;
    head = 0,tail = 0;
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&notFull,NULL);
    pthread_cond_init(&notEmpty,NULL);

}

BlockingQueue::~BlockingQueue()
{
    this->capacity = 0;
    head = 0,tail = 0;
    delete queue;
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&notFull);
    pthread_cond_destroy(&notEmpty);
}

bool BlockingQueue::push(int item)
{
    pthread_mutex_lock(&mutex);
    cout << "you want push " << item << endl;
    while((head + 1) % capacity == tail)//is full
    {
        cout << "is full,wait..." << endl;
        // push wait
        pthread_cond_wait(&notFull,&mutex);
        cout << "not full,unlock" << endl;
    }

    {
        queue[head] = item;
        head = (head + 1) % capacity;
        cout << "push " << item << endl;
        //wake up poll thread
        pthread_cond_signal(&notEmpty);
        pthread_mutex_unlock(&mutex);

        return true;
    }
}

int BlockingQueue::poll()
{
    pthread_mutex_lock(&mutex);
    int ret = 0;
    while(head == tail) // is empty
    {
        cout << "is empty,wait..." << endl;
        //poll wait
        pthread_cond_wait(&notEmpty,&mutex);
        cout << "not empty,unlock..." << endl;
    }
    {
        ret = queue[tail];
        tail = (tail + 1) % capacity;
        cout << "take " << ret << endl;
        //wake up push thread
        pthread_cond_signal(&notFull);

        pthread_mutex_unlock(&mutex);
        return ret;
    }
}


#include <iostream>
#include "include/BlockingQueue.h"
using namespace std;
BlockingQueue queue(3);

void* put(void *)
{
	queue.push(1);
	  queue.push(2);
	  queue.push(3);
	  queue.push(4);
	  queue.push(5);
	  return NULL;
}

void* take(void *)
{
	queue.poll();
	queue.poll();
	queue.poll();
	return NULL;
}


int main() {

	pthread_t put1,take1;
  pthread_create(&put1,NULL,put,0);
  pthread_create(&take1,NULL,take,0);

  void * retval;
  pthread_join(put1,&retval);
  pthread_join(take1,&retval);

	return 0;
}

以上就是小编为大家带来的Linux C++ 使用condition实现阻塞队列的方法全部内容了,希望大家多多支持脚本之家~

您可能感兴趣的文章:

相关文章

  • Linux中RPM文件操作的常用命令总结

    Linux中RPM文件操作的常用命令总结

    这篇文章主要给大家介绍了关于Linux中RPM文件操作的常用命令,文中通过示例介绍的很详细,对大家的理解和学习很有帮助,有需要的朋友们可以参考借鉴,下面来一起学习学习吧。
    2016-11-11
  • CentOS上搭建Nginx+Mono运行asp.net环境的配置方法

    CentOS上搭建Nginx+Mono运行asp.net环境的配置方法

    这篇文章主要介绍了CentOS上搭建Nginx+Mono运行asp.net环境的配置方法,需要的朋友可以参考下
    2017-03-03
  • linux环境中常用的mysql命令介绍

    linux环境中常用的mysql命令介绍

    大家好,本篇文章主要讲的是linux环境中常用的mysql命令介绍,感兴趣的同学赶快来看一看,对你有帮助的话记得收藏一下,方便下次浏览
    2021-11-11
  • 详解如何在 CentOS 7 上安装和安全配置 MariaDB 10

    详解如何在 CentOS 7 上安装和安全配置 MariaDB 10

    这篇文章主要介绍了详解如何在 CentOS 7 上安装和安全配置 MariaDB 10,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Linux下通过sed命令对kv方式的配置文件进行修改

    Linux下通过sed命令对kv方式的配置文件进行修改

    sed是unix下的面向字符流的编辑器,即stream editor, 它是面向行的,以行为单位进行处理,同时,sed是非交互式的,一旦执行便要处理完整个文件。这篇文章主要介绍了Linux下通过sed命令对kv方式的配置文件进行修改,需要的朋友可以参考下
    2018-11-11
  • Ubuntu VPS 简易安装LAMP教程

    Ubuntu VPS 简易安装LAMP教程

    LAMP是Linux、Apache、MySQL和PHP的首字母缩写词,本教程将引导你安装基于Ubuntu10.10系统的Apache2服务器,支持PHP5(mod_php)和MySQL。
    2011-02-02
  • 关于Linux搭建RabbitMQ集群环境图文详解

    关于Linux搭建RabbitMQ集群环境图文详解

    这篇文章主要介绍了关于Linux搭建RabbitMQ集群环境图文详解,RabbitMQ天然支持集群,集群是保证可靠性的一种方式,同时可以通过水平扩展以达到增加消息吞吐量能力的目的,需要的朋友可以参考下
    2023-05-05
  • Apache Commons DbUtils工具包使用介绍

    Apache Commons DbUtils工具包使用介绍

    这篇文章主要介绍了Apache Commons DbUtils工具包使用介绍,本文介绍了DBUtils是什么东西、熟悉DBUtils的一些问题、API介绍等内容,需要的朋友可以参考下
    2015-03-03
  • 关于linux中系统输入输出的管理详解

    关于linux中系统输入输出的管理详解

    这篇文章主要给大家介绍了关于linux中系统输入输出的管理,文中通过示例代码介绍的非常详细,对大家学习或者使用linux具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • 详解如何设置CentOS 7开机自动获取IP地址

    详解如何设置CentOS 7开机自动获取IP地址

    本例中以CentOS 7举例说明如何设置Linux开机自动获取IP地址和设置固定IP地址。具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03

最新评论