题目:
实现一个队列。
队列的应用场景为:一个生产者线程将int 类型的数入列,一个消费者线程将int 类型的数出列。
思路一:
这就是操作系统中介绍的PV操作,队列的一个典型的应用模式。实现这个PV操作的过程中要注意两个线程之间的通信就可以了。
代码如下:
/*-----------------------------
Copyright by yuucyf. 2011.08.25
-------------------------------*/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <iostream>
#include <queue>
using namespace std;
HANDLE g_hSemaphore = NULL; //信号量
const int g_i32PMax = 100; //生产(消费)总数
std::queue<int> g_queuePV; //生产入队,消费出队
//生产者线程
unsigned int __stdcall ProducerThread(void* pParam)
{
int i32N = 0;
while (++i32N <= g_i32PMax)
{
//生产
g_queuePV.push(i32N);
cout<<"Produce "&l