麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C > 正文

基于條件變量的消息隊列 說明介紹

2020-01-26 16:19:16
字體:
來源:轉載
供稿:網友

條件變量是線程之前同步的另一種機制。條件變量給多線程提供了一種會和的場所。當條件變量和互斥鎖一起使用時,允許線程以無競爭的方式等待特定的條件發生。這樣大大減少了鎖競爭引起的線程調度和線程等待。

     消息隊列是服務器端開發過程中繞不開的一道坎,前面,我已經實現了一個基于互斥鎖和三隊列的消息隊列,性能很不錯。博客園中的其他園主也實現了很多基于環形隊列和lock-free的消息隊列,很不錯,今天我們將要實現一個基于雙緩沖、互斥鎖和條件變量的消息隊列;這個大概也參考了一下java的blockingqueue,在前面一個博客中有簡單介紹!!基于三緩沖的隊列,雖然最大限度上解除了線程競爭,但是在玩家很少,消息很小的時候,需要添加一些buff去填充數據,這大概也是其一個缺陷吧!

     消息隊列在服務器開發過程中主要用于什么對象呢?

     1: 我想大概就是通信層和邏輯層之間的交互,通信層接受到的網絡數據,驗證封包之后,通過消息隊列傳遞給邏輯層,邏輯層將處理結果封包再傳遞給通信層!

     2:邏輯線程和數據庫IO線程的分離;數據庫IO線程負責對數據庫的讀寫更新,邏輯層對數據庫的操作,封裝成消息去請求數據庫IO線程,數據庫IO線程處理完之后,再交回給邏輯層。

     3:日志;處理模式與方式2 類似。不過日志大概是不需要返回的!

給出源代碼:

BlockingQueue.h文件

復制代碼 代碼如下:

/*
 * BlockingQueue.h
 *
 *  Created on: Apr 19, 2013
 *      Author: archy_yu
 */

#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_

#include <queue>
#include <pthread.h>

typedef void* CommonItem;

class BlockingQueue
{
public:
    BlockingQueue();

    virtual ~BlockingQueue();

    int peek(CommonItem &item);

    int append(CommonItem item);

private:

    pthread_mutex_t _mutex;

    pthread_cond_t _cond;

    std::queue<CommonItem> _read_queue;

    std::queue<CommonItem> _write_queue;

};

 
#endif /* BLOCKINGQUEUE_H_ */

BlockingQueue.cpp 文件代碼
復制代碼 代碼如下:

/*
 * BlockingQueue.cpp
 *
 *  Created on: Apr 19, 2013
 *      Author: archy_yu
 */

#include "BlockingQueue.h"

BlockingQueue::BlockingQueue()
{
    pthread_mutex_init(&this->_mutex,NULL);
    pthread_cond_init(&this->_cond,NULL);
}

BlockingQueue::~BlockingQueue()
{
    pthread_mutex_destroy(&this->_mutex);
    pthread_cond_destroy(&this->_cond);
}

int BlockingQueue::peek(CommonItem &item)
{

    if( !this->_read_queue.empty() )
    {
        item = this->_read_queue.front();
        this->_read_queue.pop();
    }
    else
    {
        pthread_mutex_lock(&this->_mutex);

        while(this->_write_queue.empty())
        {
            pthread_cond_wait(&this->_cond,&this->_mutex);
        }

        while(!this->_write_queue.empty())
        {
            this->_read_queue.push(this->_write_queue.front());
            this->_write_queue.pop();
        }

        pthread_mutex_unlock(&this->_mutex);
    }

 
    return 0;
}

int BlockingQueue::append(CommonItem item)
{
    pthread_mutex_lock(&this->_mutex);
    this->_write_queue.push(item);
    pthread_cond_signal(&this->_cond);
    pthread_mutex_unlock(&this->_mutex);
    return 0;
}

測試代碼:
復制代碼 代碼如下:

BlockingQueue _queue;

void* process(void* arg)
{

    int i=0;
    while(true)
    {
        int *j = new int();
        *j = i;
        _queue.append((void *)j);
        i ++;
    }
    return NULL;
}

int main(int argc,char** argv)
{
    pthread_t pid;
    pthread_create(&pid,0,process,0);

    long long int start = get_os_system_time();
    int i = 0;
    while(true)
    {
        int* j = NULL;
        _queue.peek((void* &)j);

        i ++;

        if(j != NULL && (*j) == 100000)
        {
            long long int end = get_os_system_time();
            printf("consume %d/n",end - start);
            break;
        }
    }

    return 0;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黄色电影免费网址 | 99精品热视频 | 国产一区视频观看 | 欧美激情综合在线 | 久草最新在线 | 国内自拍网址 | 国产小视频一区 | 国产乱一区二区三区视频 | 国产精品99精品 | 国产精品99精品 | 成人午夜视频网站 | 一级免费a| 国产日韩大片 | av在线免费看网址 | 国产精品欧美久久久久一区二区 | 国产一级毛片视频在线! | 国产亚洲在| 蜜桃网站在线观看 | 国产一级aaa全黄毛片 | 成人在线精品视频 | 免费看成人av | 国产精品久久久久久影视 | 亚洲va久久久噜噜噜久久男同 | 国产精品久久久久久久四虎电影 | 国产精品片一区二区三区 | chinese xxxxhd videos麻豆| 亚洲一区二区三区高清视频 | 国产免费最爽的乱淫视频a 毛片国产 | 中文字幕在线观看视频www | 欧美三级日本三级少妇99 | 国产午夜亚洲精品午夜鲁丝片 | 少妇的肉体k8经典 | 国产日韩精品欧美一区视频 | 99国产精品国产免费观看 | 国产精品亚洲一区二区三区久久 | 黄网站免费在线看 | 在线中文字幕不卡 | 九九热视频这里只有精品 | 亚洲精品在线观看免费 | 最近高清无吗免费看 | 国产又粗又爽又深的免费视频 |