竟然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);
}