最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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ì)列的方法全部?jī)?nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

  • 詳解CentOS7 FTP服務(wù)搭建(虛擬用戶訪問FTP服務(wù))

    詳解CentOS7 FTP服務(wù)搭建(虛擬用戶訪問FTP服務(wù))

    Liunx外部文件的傳輸,避免不了使用FTP服務(wù),所以現(xiàn)在就整理下,CentOS7環(huán)境下,F(xiàn)TP服務(wù)的搭建。有興趣的可以了解一下。
    2017-01-01
  • 判斷Unix系統(tǒng)及庫(kù)文件是32位還是64位的詳解

    判斷Unix系統(tǒng)及庫(kù)文件是32位還是64位的詳解

    這篇文章主要介紹了判斷Unix系統(tǒng)及庫(kù)文件是32位還是64位的的相關(guān)資料,這里整理下查看系統(tǒng)位數(shù)的命令,需要的朋友可以參考下
    2016-11-11
  • SpringBoot整合Activiti7的實(shí)現(xiàn)代碼

    SpringBoot整合Activiti7的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot整合Activiti7的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • apache You don''t have permission to access /test.php on this server解決方法

    apache You don''t have permission to access /test.php on thi

    這篇文章主要介紹了apache You don't have permission to access /test.php on this server解決方法,需要的朋友可以參考下
    2015-04-04
  • Apache Spark詳解(推薦)

    Apache Spark詳解(推薦)

    spark.executor.memory以及其他Spark配置參數(shù)既可以在代碼中設(shè)置,也可以在其他幾個(gè)地方設(shè)置,具體取決于你的使用場(chǎng)景和偏好,這篇文章主要介紹了Apache Spark詳解,需要的朋友可以參考下
    2024-07-07
  • linux服務(wù)器CPU飆高排查分析

    linux服務(wù)器CPU飆高排查分析

    系統(tǒng)cpu飆高,尤其對(duì)于后端人員來說,其實(shí)應(yīng)該學(xué)會(huì)排查,這樣也算是綜合能力的體現(xiàn);那么當(dāng)出現(xiàn)了cpu嚴(yán)重飆高的時(shí)候怎么排查呢?感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • linux常用命令小結(jié)之yum、源碼安裝

    linux常用命令小結(jié)之yum、源碼安裝

    這篇文章主要介紹了linux常用命令小結(jié)之yum、源碼安裝的相關(guān)資料,需要的朋友可以參考下
    2018-04-04
  • 關(guān)于Windows 不能在 本地計(jì)算器 啟動(dòng) Apache2(phpstudy)

    關(guān)于Windows 不能在 本地計(jì)算器 啟動(dòng) Apache2(phpstudy)

    今天在自己的本子上準(zhǔn)備放多個(gè)虛擬站點(diǎn)。用的是#phpstudy#。在軟件自身的站點(diǎn)設(shè)置中,根據(jù)提示添加的多站點(diǎn)無效不知道是否和我的系統(tǒng)是Win7有關(guān)
    2012-09-09
  • 使用ssh-keygen,實(shí)現(xiàn)免密碼登陸linux的方法

    使用ssh-keygen,實(shí)現(xiàn)免密碼登陸linux的方法

    下面小編就為大家?guī)硪黄褂胹sh-keygen,實(shí)現(xiàn)免密碼登陸linux的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • Linux:FTP工具及SSH遠(yuǎn)程連接工具的使用方式

    Linux:FTP工具及SSH遠(yuǎn)程連接工具的使用方式

    這篇文章主要介紹了Linux:FTP工具及SSH遠(yuǎn)程連接工具的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評(píng)論

炎陵县| 江都市| 延长县| 洪泽县| 蓝田县| SHOW| 廊坊市| 巫溪县| 鲁山县| 当雄县| 枝江市| 浮梁县| 青浦区| 梁河县| 新宁县| 邢台市| 和田县| 莎车县| 井冈山市| 祁门县| 应用必备| 安义县| 凤凰县| 黄梅县| 贺兰县| 镶黄旗| 静宁县| 松桃| 喀喇| 葵青区| 卫辉市| 沛县| 突泉县| 新龙县| 赤水市| 黄冈市| 密云县| 连平县| 郯城县| 尚义县| 长宁县|