多线程环境下安全的消息队列存取---利用信号量

       前面几篇博文中, 我们一直在讨论异质链表消息队列问题, 在本文中, 我们继续来探讨这一问题, 在多线程环境下, 考虑线程同步问题, 代码如下(我用Sleep(20);来故意捣乱):

 

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

#define Rectangle MyRectangle  // 避免Rectangle与Windows中的Rectangle冲突

HANDLE g_hSemp = NULL;  // 信号量handle

// 对象的id值
typedef enum
{
	ErrorId = -1,
	IntegerId = 1,
	PointId = 2,
	RectangeId = 3,
}ObjectID;


// 基类
struct Basic
{
	ObjectID id;
	virtual Basic *copy() = 0; // 纯虚函数
};

// 整数类
struct Integer : public Basic
{
	int a;

	Basic *copy()
	{
		Integer *p = new Integer;
		p->a = ((Integer*)this)->a;
		p->id = ((Integer*)this)->id;

		return p;
	}
};

// 点类
struct Point : public Basic
{
	int x;
	int y;

	Basic *copy()
	{
		Point *p = new Point;
		p->x = ((Point*)this)->x;
		p->y = ((Point*)this)->y;
		p->id = ((Point*)this)->id;

		return p;
	}
};

// 矩形类
struct Rectangle : public Basic
{
	Point point;
	int width;
	int height;

	Basic *copy()
	{
		Rectangle *p = new Rectangle;
		p->point.x = ((Rectangle*)this)->point.x;
		p->point.y = ((Rectangle*)this)->point.y;
		p->width = ((Rectangle*)this)->width;
		p->height = ((Rectangle*)this)->height;
		p->id = ((Rectangle*)this)->id;

		return p;
	}
};

// 抽象对象的共同点, 构造成新的结点, 便于链接
typedef struct node
{
	node *next;
	Basic *pBasic;
}Node;


Node *head = NULL;  // 指向第一结点(采用不带头结点的链表)


// 往链式消息队列中塞消息
Node *addToMsgQueue(Basic* pb)
{
	WaitForSingleObject(g_hSemp, INFINITE);
	printf("add\n");

	Node *pn = new Node;
	Node *qn = NULL;
	Basic *p = pb->copy(); // 多态性

	if(NULL == head)
	{
		Sleep(20);  
		head = pn;
	}
	else
	{
		qn = head;
		while(NULL != qn->next)
		{
			Sleep(20);  
			qn = qn->next;
		}

		Sleep(20);  
		qn->next = pn;
	}

	pn->pBasic = p;   // 千万别忘记啊
	pn->next = NULL;  // 千万别忘记啊

	
	ReleaseSemaphore(g_hSemp, 1, NULL);  

	return head;
}


// 从链式消息队列中取出消息(结点)
Node *getMsgFromQueue()
{
	WaitForSingleObject(g_hSemp, INFINITE);  
	printf("get\n");


	if(NULL == head)
	{
		Sleep(20);  
		ReleaseSemaphore(g_hSemp, 1, NULL);  
		return NULL;
	}

	Node *pn = head;
	head = head->next;
	Sleep(20);  

	ReleaseSemaphore(g_hSemp, 1, NULL);  
	return pn;
} 


// 线程函数
DWORD WINAPI ThreadFun(LPVOID pM)
{
	Node *p = NULL;

	// 从消息队列中取出消息
	while(1)
	{
		p = getMsgFromQueue();
		if(NULL == p)
		{
			Sleep(100);
			continue;
		}

		// 对指针进行还原
		switch(p->pBasic->id) 
		{
			case IntegerId:
			{
				cout << ((Integer*)(p->pBasic))->a << endl;
				break;
			}
			case PointId:
			{
			
				cout << ((Point *)(p->pBasic))->x << endl;
				cout << ((Point *)(p->pBasic))->y << endl;
				break;
			}
			case RectangeId:
			{
				cout << ((Rectangle *)(p->pBasic))->point.x << endl;
				cout << ((Rectangle *)(p->pBasic))->point.y << endl;
				cout << ((Rectangle *)(p->pBasic))->width << endl;
				cout << ((Rectangle *)(p->pBasic))->height << endl;
				break;
			}
			default:
			{
				break;
			}
		}
	}

	return 0;
}

// 主线程
int main()
{
	HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);
    CloseHandle(handle);

	g_hSemp = CreateSemaphore(NULL, 1, 10, NULL);  
	


	// 定义三个对象并赋值
	Integer i;
	Point po;
	Rectangle rect;

	i.id = IntegerId;
	po.id = PointId;
	rect.id = RectangeId;
	
	i.a = 11;
	po.x = 22;
	po.y = 33;

	rect.point.x = 44;
	rect.point.y = 55;
	rect.width = 66;
	rect.height = 77;

	// 塞入消息队列
	while(1)
	{	
		addToMsgQueue(&i);
		addToMsgQueue(&po);
		addToMsgQueue(&rect);
		Sleep(2000);
	}

	CloseHandle(g_hSemp);
	return 0;
}

       测试过十几次, 没发现程序有什么问题。 如果大家有不同意见, 可以提出。 

 

       貌似该睡觉了啊。
 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值