循环队列的实现

我认为用数组做队列,首先要考虑的是队列的容量问题。解决队列的容量大家都是用循环队列的方式。但是使用循环队列需要考虑的问题是下标的控制。还有就是线程安全的问题。不能够出现对同一位置多次入队,多次出队问题。
再考虑到以上问题的同时,在下又想到一个问题就是 一个线程入队的同时另一个线程能不能出队那?要衡量这个问题就需要考虑,当同时出现时,这个线程的操作是否影响另一个线程的操作问题。思虑再三,个人觉得出队和入队修改的下标并不是同一个变量,只是读取了下下标变量,因为读写是可以同时的所以应该是没问题的。除了上述解释,又想到了其实这个可以在宏观上大概理解为 一个线程在往文件里面写 一个在从文件里面读,只是不能存在多个一起写一起从相同偏移读。

所以代码实现如下(windows)

#include “stdafx.h”
#include <windows.h>
#include < iostream>
#include < fstream>
using namespace std;

void writeFile(LPCSTR path,LPCSTR content)
{
std::ofstream outfile(path,ios::app);
outfile << content << endl;
}
//个人认为队列 可以存在 一个线程 入队的同时 一个线程出队
//临界区
static CRITICAL_SECTION _wcritical; //写
static CRITICAL_SECTION _rcritical; //读
static CONDITION_VARIABLE _wconditon; //写条件变量
static CONDITION_VARIABLE _rcondition;//读条件变量
template< typename T>
class CQUEUE{
public:
//创建队列
static CQUEUE* createQueue(int length)
{
CQUEUE* lpQueue = new CQUEUE;
lpQueue->iheadIndex = 0;
lpQueue->itailIndex = 0;
lpQueue->length = length;
lpQueue->array = new T[length];
memset(lpQueue->array,0,sizeof(T)*length);
InitializeCriticalSection(&_wcritical);
InitializeCriticalSection(&_rcritical);
return lpQueue;
}
//入栈
void pushQueue(T v_value)
{
EnterCriticalSection(&_wcritical); //控制不能多个线程同时入队
//队列是否为满
while(length == itailIndex - iheadIndex)
{
//队满 挂起当前线程
SleepConditionVariableCS(&_wconditon,&_wcritical,INFINITE);
}
array[(itailIndex++) % length] = v_value;
WakeConditionVariable(&_rcondition);
LeaveCriticalSection(&_wcritical);
}
T popQueue()
{
EnterCriticalSection(&_rcritical);
T rtValue = 0;
while(itailIndex == iheadIndex)
{
//队空
SleepConditionVariableCS(&_rcondition,&_rcritical,INFINITE);
}
rtValue = array[(iheadIndex++)%length];
WakeConditionVariable(&_wconditon);
LeaveCriticalSection(&_rcritical);
return rtValue;
}
int iheadIndex;
int itailIndex;
int length;
T *array;
};

DWORD WINAPI PushThreadProc(LPVOID lpParam)
{
int iCount = 1000;
int number = (int)lpParam;
while(iCount–)
{
g_pQueue->pushQueue(number);
printf(“pushnum:%d ThreadId:%d pushvalue:%d\n”,number,GetCurrentThreadId(),number);
/std::string strPath = “C:/Users/Administrator/Desktop/”;
char buf[10];
memset(buf,0,sizeof(buf));
itoa(number,buf,10);
strPath += buf;
strPath += “.txt”;
string strContent = “pushnum:”;
memset(buf,0,sizeof(buf));
itoa(iCount,buf,10);
strContent += buf;
writeFile(strPath.c_str(),strContent.c_str());
/ //输出文件日志 观察是否出现死锁用的
}
return 0;
}
DWORD WINAPI PopThreadProc(LPVOID lpParam)
{
int number = (int)lpParam;
int iCount = 1000;
while(iCount–)
{
int rtValue = g_pQueue->popQueue();
printf(“popnum:%d ThreadId:%d popvalue:%d\n”,number,GetCurrentThreadId(),rtValue);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
g_pQueue = CQUEUE::createQueue(10);
int number1 = 1,number2 = 2,number3 = 3,number4 = 4,number5 = 5,number6 = 6;
CreateThread(NULL,0,PushThreadProc,&number1,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number2,0,NULL);
CreateThread(NULL,0,PushThreadProc,&number3,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number4,0,NULL);
CreateThread(NULL,0,PushThreadProc,&number5,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number6,0,NULL);
char c = 0;
cin >> c;
return 0;
}

对以上的代码简单的说下,以上的队列是线程安全的,使用了类模版,考虑到的是因为存储的数据类型可以不一样。存储类型要有构造函数 因为在分配空间的时候是调用的该类型的构造。(其实一般也不用,因为基本类型有隐式的转换,一般的类和结构体都有默认的构造)
2
int number1 = 1,number2 = 2,number3 = 3,number4 = 4,number5 = 5,number6 = 6;
CreateThread(NULL,0,PushThreadProc,&number1,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number2,0,NULL);
CreateThread(NULL,0,PushThreadProc,&number3,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number4,0,NULL);
CreateThread(NULL,0,PushThreadProc,&number5,0,NULL);
CreateThread(NULL,0,PopThreadProc,&number6,0,NULL);
这一块为什么不用 循环?
像这个样子
for(int i= 0;i < 3;i++)
{
CreateThread(NULL,0,PushThreadProc,&i,0,NULL);
CreateThread(NULL,0,PopThreadProc,&i,0,NULL);
}
因为传递的是指针 可能出现 当主线程 i已经+1后 开启的线程才运行 那么就会出现 本来考虑的是传 1 进去,但是由于 线程执行的慢 此时读取到的就是2.所以不是本来的意图。
以上实测没什么问题。

linux下

#include <pthread.h>
#include <iostream>
using namespace std;

//循环队列实现

//读写互斥量
static pthread_mutex_t _wMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t _rMutex = PTHREAD_MUTEX_INITIALIZER;
//设置 读写条件变量
static pthread_cond_t _wCond = PTHREAD_COND_INITIALIZER;
static pthread_cond_t _rCond = PTHREAD_COND_INITIALIZER;

template<typename T>
class CQueue
{
private:
	/* data */
	int iheadIndex;
	int itailIndex;
	int length;
	T* 	array;
public:
	CQueue(int length/* args */);
	~CQueue();

	//入队
	void PushQueue(T v_value);
	//出队
	T PopQueue();
	bool IsFull();
	bool IsEmpty();
};

template<typename T>
CQueue<T>::CQueue(int length/* args */)
{
	iheadIndex = 0;
	itailIndex = 0;
	this->length = length;
	array = new T[length];
}

template<typename T>
CQueue<T>::~CQueue()
{
	if(array)
	{
		delete array;
		array = NULL;
	}
}

template<typename T>
void CQueue<T>::PushQueue(T v_value)
{
	pthread_mutex_lock(&_wMutex);
	//队满?
	while(IsFull())
	{
		pthread_cond_wait(&_wCond,&_wMutex);
	}

	array[(itailIndex++) % length] = v_value;
	pthread_cond_signal(&_rCond);		//唤醒pop 线程
	pthread_mutex_unlock(&_wMutex);
}
template<typename T>
T CQueue<T>::PopQueue()
{
	pthread_mutex_lock(&_rMutex);
	T rtValue = 0;
	while(IsEmpty())
	{
		pthread_cond_wait(&_rCond,&_rMutex);
	}
	rtValue = array[(iheadIndex++) % length];
	pthread_cond_signal(&_wCond);
	pthread_mutex_unlock(&_rMutex);
	return rtValue;
}

template<typename T>
bool CQueue<T>::IsFull()
{
	return length == itailIndex - iheadIndex;
}

template<typename T>
bool CQueue<T>::IsEmpty()
{
	return iheadIndex == itailIndex;
}

CQueue<int>* g_Queue = NULL;

void* pushThread(void* pParam)
{
	int i = 1000;
	while(i--)
	{
		g_Queue->PushQueue(*(int*)pParam);
	}
	return 0;
}

void* popThread(void* pParam)
{
	int i = 1000;
	while(i--)
	{
		cout << "pop num:" << *(int*)pParam << "  value:"<< g_Queue->PopQueue() << endl;
	}
	return 0;
}

int main()
{
	g_Queue = new CQueue<int>(20);
	pthread_t test1 = 0,test2 = 0;
	int i1 = 0,i2 = 1,i3 = 2,i4 = 3,i5=4,i6=5;
	// for(int i = 0;i < 3;i++)
	// {
	// 	pthread_create(&test1,NULL,&pushThread,&i);
	// 	pthread_create(&test2,NULL,&popThread,&i+1);
	// }

	pthread_create(&test1,NULL,&pushThread,&i1);
	pthread_create(&test2,NULL,&popThread,&i2);
	pthread_create(&test1,NULL,&pushThread,&i3);
	pthread_create(&test2,NULL,&popThread,&i4);
	pthread_create(&test1,NULL,&pushThread,&i5);
	pthread_create(&test2,NULL,&popThread,&i6);

	char ch = 0;
	cin >> ch;
	delete g_Queue;
	g_Queue = NULL;
	return 0;
}

以上对于队列来说,存在一个问题
没有退出的条件:
应该在
while(IsEmpty())
{
pthread_cond_wait(&_rCond,&_rMutex);
}

while(IsFull())
{
pthread_cond_wait(&_wCond,&_wMutex);
}
都要加一个判断当前是否退出,如果没加会造成一些问题。改为
template
T CQueue::PopQueue()
{
pthread_mutex_lock(&_rMutex);
T rtValue = 0;
while(IsEmpty() && !g_exit)
{
pthread_cond_wait(&_rCond,&_rMutex);
}
if(g_exit)
{
pthread_cond_broadcast(&_wCond);
pthread_cond_broadcast(&_rCond);
pthread_mutex_unlock(&_rMutex);
}
else
{
rtValue = array[(iheadIndex++) % length];
pthread_cond_signal(&_wCond);
pthread_mutex_unlock(&_rMutex);
}
return rtValue;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值