1. 程式人生 > >Boost無鎖佇列

Boost無鎖佇列

在開發接收轉發agent時,採用了多執行緒的生產者-消費者模式,用了加互斥鎖的方式來實現執行緒同步。互斥鎖會阻塞執行緒,所以壓測時,效率並不高。所以想起用無鎖佇列來實現,效能確實提升了。

首先介紹下lock-free和wait-free的區別:

阻塞演算法可能會出現整個系統都掛起的情況(佔有鎖的執行緒被中斷,無法釋放所,那麼所有試圖爭用這個鎖的執行緒會被掛起),系統中的所有執行緒全部餓死。

無鎖演算法可以保證系統中至少有一個執行緒處於工作狀態,但是還是可能有執行緒永遠搶不到資源而被餓死。

無等待演算法保證系統中的所有執行緒都能處於工作狀態,沒有執行緒會被餓死,只要時間夠,所有執行緒都能結束。相比於無鎖演算法,無等待演算法有更強的保證。

一. 用互斥鎖實現單生產者-單消費者

#include <string>
#include <sstream>
#include <list>
#include <pthread.h>
#include <iostream>
#include <time.h>

using namespace std;

int producer_count = 0;
int consumer_count = 0;

list<string> product;
list<string> consumer_list;
pthread_mutex_t mutex;

const
int iterations = 10000; //是否生產完畢標誌 bool done = false; void* producer(void* args) { for (int i = 0; i != iterations; ++i) { pthread_mutex_lock(&mutex); int value = ++producer_count; stringstream ss; ss<<value; product.push_back(ss.str()); //cout<<"list push:"<<ss.str()<<endl;
pthread_mutex_unlock(&mutex); } return 0; } //消費函式 void* consumer(void* args) { //當沒有生產完畢,則邊消費邊生產 while (!done) { pthread_mutex_lock(&mutex); if(!product.empty()){ consumer_list.splice(consumer_list.end(), product); pthread_mutex_unlock(&mutex); while(!consumer_list.empty()){ string value = consumer_list.front(); consumer_list.pop_front(); //cout<<"list pop:"<<value<<endl; ++consumer_count; } }else{ pthread_mutex_unlock(&mutex); } } //如果生產完畢,則消費 while(!consumer_list.empty()){ string value = consumer_list.front(); consumer_list.pop_front(); //cout<<"list pop:"<<value<<endl; ++consumer_count; } return 0; } int main(int argc, char* argv[]) { struct timespec time_start={0, 0},time_end={0, 0}; clock_gettime(CLOCK_REALTIME, &time_start); pthread_t producer_tid; pthread_t consumer_tid; pthread_mutex_init (&mutex,NULL); pthread_create(&producer_tid, NULL, producer, NULL); pthread_create(&consumer_tid, NULL, consumer, NULL); //等待生產者生產完畢 pthread_join(producer_tid, NULL); //可以消費標誌 done = true; //主執行緒不等生產執行緒完畢就設定done標記 cout << "producer done" << endl; //輸出以觀察主執行緒和各子執行緒的執行順序 //等待消費者結束 pthread_join(consumer_tid, NULL); clock_gettime(CLOCK_REALTIME, &time_end); long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000; cout<<"===========cost time:"<<cost<<"us==========="<<endl; cout << "produced " << producer_count << " objects." << endl; cout << "consumed " << consumer_count << " objects." << endl; }

生產消費10000個string型別的資料,耗時:58185us

二. Boost庫的無鎖佇列

boost.lockfree實現了三種無鎖資料結構:
boost::lockfree::queue
alock-free multi-produced/multi-consumer queue
一個無鎖的多生產者/多消費者佇列,注意,這個queue不支援string型別,支援的資料型別要求:
- T must have a copy constructor
- T must have a trivial assignment operator
- T must have a trivial destructor

boost::lockfree::stack
alock-free multi-produced/multi-consumer stack
一個無鎖的多生產者/多消費者棧,支援的資料型別要求:
- T must have a copy constructor

boost::lockfree::spsc_queue
await-free single-producer/single-consumer queue (commonly known as ringbuffer)
一個無等待的單生產者/單消費者佇列(通常被稱為環形緩衝區),支援的資料型別要求:
- T must have a default constructor
- T must be copyable

三. Queue示例

這裡實現的還是單生產者-單消費者。

#include <pthread.h>
#include <boost/lockfree/queue.hpp>
#include <iostream>
#include <time.h>
#include <boost/atomic.hpp>

using namespace std;

//生產數量
boost::atomic_int producer_count(0);
//消費數量
boost::atomic_int consumer_count(0);
//佇列
boost::lockfree::queue<int> queue(512);


//迭代次數
const int iterations = 10000;

//生產函式
void* producer(void* args)
{
    for (int i = 0; i != iterations; ++i) {
        int value = ++producer_count;
        //原子計數————多執行緒不存在計數不上的情況       
        //若沒有進入佇列,則重複推送
        while(!queue.push(value));
        //cout<<"queue push:"<<value<<endl;
    }
    return 0;
}

//是否生產完畢標誌
boost::atomic<bool> done (false);

//消費函式
void* consumer(void* args)
{
    int value;
    //當沒有生產完畢,則邊消費邊生產
    while (!done) {
        //只要能彈出元素,就消費
        while (queue.pop(value)) {
            //cout<<"queue pop:"<<value<<endl;
            ++consumer_count;
        }
    }
    //如果生產完畢,則消費
    while (queue.pop(value)){
        //cout<<"queue pop:"<<value<<endl;
        ++consumer_count;
    }
    return 0;
}

int main(int argc, char* argv[])
{
    cout << "boost::lockfree::queue is ";
    if (!queue.is_lock_free())
        cout << "not ";
    cout << "lockfree" << endl;

    struct timespec time_start={0, 0},time_end={0, 0};
    clock_gettime(CLOCK_REALTIME, &time_start);

    pthread_t producer_tid;
    pthread_t consumer_tid;

    pthread_create(&producer_tid, NULL, producer, NULL);
    pthread_create(&consumer_tid, NULL, consumer, NULL);

    //等待生產者生產完畢
    pthread_join(producer_tid, NULL);
    //可以消費標誌
    done = true;     //主執行緒不等生產執行緒完畢就設定done標記
    cout << "producer done" << endl;    //輸出以觀察主執行緒和各子執行緒的執行順序

    //等待消費者結束
    pthread_join(consumer_tid, NULL);
    clock_gettime(CLOCK_REALTIME, &time_end);

    long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000;

    cout<<"===========cost time:"<<cost<<"us==========="<<endl;

    //輸出生產和消費數量
    cout << "produced " << producer_count << " objects." << endl;
    cout << "consumed " << consumer_count << " objects." << endl;

    return 0;
}

生產消費10000個int型別的資料,耗時:3963us
stack與queue類似,只不過是先進後出。

四. Waitfree Single-Producer/Single-Consumer Queue無等待單生產者/單消費者佇列

#include <pthread.h>
#include <iostream>
#include <time.h>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/atomic.hpp>

using namespace std;

int producer_count = 0;
boost::atomic_int consumer_count (0);

boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue;

const int iterations = 10000;

void* producer(void* args)
{
    for (int i = 0; i != iterations; ++i) {
        int value = ++producer_count;
        while(!spsc_queue.push(value));
        //cout<<"queue push:"<<value<<endl;
    }
    return 0;
}

//是否生產完畢標誌
boost::atomic<bool> done (false);

//消費函式
void* consumer(void* args)
{
    int value;
    //當沒有生產完畢,則邊消費邊生產
    while (!done) {
        //只要能彈出元素,就消費
        while (spsc_queue.pop(value)) {
            //cout<<"queue pop:"<<value<<endl;
            ++consumer_count;
        }
    }
    //如果生產完畢,則消費
    while (spsc_queue.pop(value)){
        //cout<<"queue pop:"<<value<<endl;
        ++consumer_count;
    }
    return 0;
}

int main(int argc, char* argv[])
{
    using namespace std;
    cout << "boost::lockfree::queue is ";
    if (!spsc_queue.is_lock_free())
        cout << "not ";
    cout << "lockfree" << endl;

    struct timespec time_start={0, 0},time_end={0, 0};
    clock_gettime(CLOCK_REALTIME, &time_start);

    pthread_t producer_tid;
    pthread_t consumer_tid;

    pthread_create(&producer_tid, NULL, producer, NULL);
    pthread_create(&consumer_tid, NULL, consumer, NULL);

    //等待生產者生產完畢
    pthread_join(producer_tid, NULL);
    //可以消費標誌
    done = true;     //主執行緒不等生產執行緒完畢就設定done標記
    cout << "producer done" << endl;    //輸出以觀察主執行緒和各子執行緒的執行順序

    //等待消費者結束
    pthread_join(consumer_tid, NULL);
    clock_gettime(CLOCK_REALTIME, &time_end);

    long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000;

    cout<<"===========cost time:"<<cost<<"us==========="<<endl;

    cout << "produced " << producer_count << " objects." << endl;
    cout << "consumed " << consumer_count << " objects." << endl;
}

生產消費10000個int型別的資料,耗時:1832us
如果把int改為string型別,耗時:28788us

五.效能對比

這裡寫圖片描述
從上面可以看出在單生產者-單消費者模式下,spsc_queue比queue效能好,無鎖佇列比互斥鎖的方式效能也要好。