欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux C++ 使用condition實(shí)現(xiàn)阻塞隊(duì)列的方法

 更新時(shí)間:2017年01月06日 10:04:49   投稿:jingxian  
下面小編就為大家?guī)硪黄狶inux C++ 使用condition實(shí)現(xiàn)阻塞隊(duì)列的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

實(shí)例如下:

/*
 * 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;
}

以上就是小編為大家?guī)淼腖inux C++ 使用condition實(shí)現(xiàn)阻塞隊(duì)列的方法全部內(nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

  • Linux 內(nèi)核空間與用戶空間實(shí)現(xiàn)與分析

    Linux 內(nèi)核空間與用戶空間實(shí)現(xiàn)與分析

    這篇文章主要介紹了Linux 內(nèi)核空間與用戶空間實(shí)現(xiàn)與分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • Linux配置防火墻,開啟80、3306端口的實(shí)例方法

    Linux配置防火墻,開啟80、3306端口的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于Linux配置防火墻,開啟80端口、3306端口的相關(guān)內(nèi)容,需要的朋友們參考下。
    2020-02-02
  • linux服務(wù)器ubuntu定時(shí)任務(wù)cron設(shè)置每秒執(zhí)行

    linux服務(wù)器ubuntu定時(shí)任務(wù)cron設(shè)置每秒執(zhí)行

    這篇文章主要介紹了linux服務(wù)器ubuntu定時(shí)任務(wù)cron設(shè)置每秒執(zhí)行,使用 cron 時(shí),有一些注意事項(xiàng)可以幫助你確保任務(wù)按預(yù)期執(zhí)行,并減少潛在的問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2024-02-02
  • 詳解Centos7中Nginx開機(jī)自啟動(dòng)的解決辦法

    詳解Centos7中Nginx開機(jī)自啟動(dòng)的解決辦法

    本篇文章主要介紹了詳解Centos7中Nginx開機(jī)自啟動(dòng)的解決辦法,具有一定的參加價(jià)值,有興趣的可以了解一下。
    2017-03-03
  • 開啟Selinux遇到的坑及解決

    開啟Selinux遇到的坑及解決

    這篇文章主要介紹了開啟Selinux遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Linux中特殊權(quán)限SUID、SGID與SBIT的深入講解

    Linux中特殊權(quán)限SUID、SGID與SBIT的深入講解

    linux對文件的權(quán)限管理簡直是讓人嘆為觀止,所以這篇文章主要給大家介紹了關(guān)于Linux中特殊權(quán)限SUID、SGID與SBIT的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Linux加載vmlinux調(diào)試

    Linux加載vmlinux調(diào)試

    今天小編就為大家分享一篇關(guān)于Linux加載vmlinux調(diào)試,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫的簡單方法

    linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫的簡單方法

    在本篇文章中我們給大家整理了一些關(guān)于linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫的簡單方法,有需要的朋友們可以學(xué)習(xí)下。
    2018-09-09
  • Vim中的幾種文件備份方法總結(jié)

    Vim中的幾種文件備份方法總結(jié)

    最近在MCTF上看到了Vim的undo備份,順手學(xué)習(xí)了下 Vim 的幾種備份機(jī)制,所以這篇文章主要給大家介紹了關(guān)于Vim中的幾種文件備份,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • Linux環(huán)境使用g++編譯C++方法總結(jié)

    Linux環(huán)境使用g++編譯C++方法總結(jié)

    本篇文章給大家分享了在Linux環(huán)境中用g++編譯C++的方法以及相關(guān)實(shí)例代碼分享,有興趣的朋友學(xué)習(xí)下。
    2018-03-03

最新評論