自己写的基于链表的并发安全队列模板类concurrent_queue

竟然VS2008没有concurrent_queue.h,就只能自己写一个了,现在这样估计都够用了。

#pragma once
#include <Windows.h>

template<typename T>
class concurrent_queue
{
public:
    typedef struct Node
    {
        T* data;
        Node* next;
    }*iterator;

    concurrent_queue();
    concurrent_queue( concurrent_queue<T> & queue_copy);
    ~concurrent_queue();

    void push(const T & _Src);
    bool try_pop( T& _Dest );
    size_t unsafe_size();
    bool empty();
    void clear();

    T at(int index);
    iterator earse(int index);

    iterator unsafe_begin();
    iterator unsafe_end();

    void operator=( concurrent_queue<T>& queue);
private:
    iterator head;
    CRITICAL_SECTION cs;
};

template<typename T>
typename concurrent_queue<T>::iterator concurrent_queue<T>::earse( int index )
{
    EnterCriticalSection(&cs);
    Node* p = head;
    Node* q = head;
    while ( index-- > 0 )
    {    
        head = head->next;
        if (head == NULL)
        {
            head = q;
            LeaveCriticalSection(&cs);
            return NULL;
        }
    }
    head->next = head->next->next;
    p = head->next;
    head = q;
    LeaveCriticalSection(&cs);
    return p;
}

template<typename T>
T concurrent_queue<T>::at( int index )
{
    EnterCriticalSection(&cs);
    Node* p = head;
    while ( index-- >= 0 )
    {    
        p = p->next;
    }
    LeaveCriticalSection(&cs);
    return *p->data;
}

template<typename T>
void concurrent_queue<T>::operator=( concurrent_queue<T>& queue )
{
    EnterCriticalSection(&cs);
    delete head;
    head = new Node;
    head->next = NULL;
    Node* p = head;
    iterator pit = queue.unsafe_begin();
    while (pit != queue.unsafe_end())
    {
        Node *temp = new Node;
        temp->data = (T*)malloc(sizeof(T));
        memcpy(temp->data,pit->data,sizeof(T));
        temp->next = NULL;
        p->next = temp;
        p = p->next;
        pit = pit->next;
    }
    LeaveCriticalSection(&cs);
}

template<typename T>
typename concurrent_queue<T>::iterator concurrent_queue<T>::unsafe_end()
{
    return NULL;
}

template<typename T>
typename concurrent_queue<T>::iterator concurrent_queue<T>::unsafe_begin()
{
    return head->next;
}


template<class T>
concurrent_queue<T>::concurrent_queue( concurrent_queue<T> & queue_copy)
{
    InitializeCriticalSection(&cs);
    EnterCriticalSection(&cs);
    head=new Node;
    head->data = new T;
    head->next=NULL;
    Node* p = head;
    iterator pit = queue_copy.unsafe_begin();
    while (pit != queue_copy.unsafe_end())
    {
        Node *temp = new Node;
        temp->data = (T*)malloc(sizeof(T));
        memcpy(temp->data,pit->data,sizeof(T));
        temp->next = NULL;
        p->next = temp;
        p = p->next;
        pit = pit->next;
    }
    LeaveCriticalSection(&cs);
}

template<typename T>
void concurrent_queue<T>::clear()
{
    EnterCriticalSection(&cs);
    delete head;
    head = new Node;
    head->next = NULL;
    LeaveCriticalSection(&cs);
}

template<typename T>
bool concurrent_queue<T>::empty()
{
    EnterCriticalSection(&cs);
    bool ret = head->next == NULL;
    LeaveCriticalSection(&cs);
    return ret;
}

template<typename T>
size_t concurrent_queue<T>::unsafe_size()
{
    EnterCriticalSection(&cs);
    size_t count=0;
    Node* p = head;
    while (p->next != NULL)
    {
        count++;
        p = p->next;
    }
    LeaveCriticalSection(&cs);
    return count;
}

template<typename T>
bool concurrent_queue<T>::try_pop( T& _Dest )
{
    EnterCriticalSection(&cs);
    if (head->next == NULL)
    {
        LeaveCriticalSection(&cs);
        return false;
    }
    memcpy(&_Dest,head->next->data,sizeof(T));
    head->next = head->next->next;
    LeaveCriticalSection(&cs);
    return true;
}

template<typename T>
void concurrent_queue<T>::push( const T & _Src )
{
    EnterCriticalSection(&cs);
    Node *p = head;
    while (p->next != NULL)
    {
        p = p->next;
    }
    Node *temp = new Node;
    temp->data = (T*)malloc(sizeof(_Src));
    memcpy(temp->data,(void*)&_Src,sizeof(_Src));
    temp->next = NULL;
    p->next = temp;
    LeaveCriticalSection(&cs);
}



template<typename T>
concurrent_queue<T>::~concurrent_queue()
{
    clear();
    delete head;
    head=NULL;
    DeleteCriticalSection(&cs);
}

template<typename T>
concurrent_queue<T>::concurrent_queue()
{
    InitializeCriticalSection(&cs);
    EnterCriticalSection(&cs);
    head=new Node;
    head->data = new T;
    head->next=NULL;
    LeaveCriticalSection(&cs);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值