Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 不用线程库而用宏来模拟多线程中的PV操作
【标  题】:不用线程库而用宏来模拟多线程中的PV操作
【关键字】:PV
【来  源】:http://blog.csdn.net/SpriteLW/archive/2006/12/29/1467801.aspx

不用线程库而用宏来模拟多线程中的PV操作

Your Ad Here
今天翻了一下自己以前写的程序,发现以一个很有趣的代码―――用宏来写的多线程序程序。那是我女朋友的作业,用PV操作实现“生产者消费者”程序,她的老师说不能使用库,当时我的第一个想法是“怎么可能呢”。但为了在女朋友面前展示我的魅力,我答应了她帮她做。
当时正处C发旺期,,而且在女朋友的影响下更发挥了小宇宙,于是就想到了用宏,以下是整个代码:
#include <stdlib.h>
#include 
<process.h>
#include 
<windows.h>
#include 
<iostream>
#include 
<time.h>
 
using namespace std;
#define PRODUCER 0
#define CONSUMER 1
 
int empty = 1;
int full = 0;
int who;
 
#define P(X) 
{
       X
--;
       
switch(who)
       {
       
case PRODUCER:
              
if(X < 0)
              {
                     cout
<<"Producer is waiting"<<endl;
                     X 
= 0;
                     
goto WAIT;
              }
              
break;
       
case CONSUMER:
              
if(X < 0)
              {
                     cout
<<"Consumer is waiting"<<endl;
                     X 
= 0;
                     
goto WAIT;
              }
              
break;
       }
}
 
#define V(X) 
{
       X
++;
       
switch(who)
       {
       
case PRODUCER:
              
goto LEAVE;
              
break;
       
case CONSUMER:
              
goto LEAVE;
              
break;
       }
}
int buffer = 1;
 
void producer()
{
       who 
= PRODUCER;
       P(empty)
              cout
<<"producer put a thing in the buffer"<<endl;
              buffer 
= 1;
       V(full);
 
LEAVE:
       
return;
WAIT:
       
return;
}
 
void consumer()
{
       who 
= CONSUMER;
       P(full)
              cout
<<"consumer get a thing in the buffer"<<endl;
              buffer 
= 0;      
       V(empty);
LEAVE:
       
return;
WAIT:
       
return;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
       srand((unsigned)time(NULL));
              
       
while(1)
       {
              Sleep(
500);
              
switch(rand()%2)
              {
              
case 0:
                     producer();
                     
break;
              
case 1:
                     consumer();
                     
break;
              }
       }
       
return 0;
}
 
相应地,我也给出了如果用库的代码:
#include <windows.h>
#include 
<process.h>
#include 
<time.h>
 
using namespace std;
 
 
HANDLE full;
HANDLE empty;
 
void Producer(void*)
{
       cout
<<"a Producer come in"<<endl;
       WaitForSingleObject(empty,INFINITE);
//P(empty)
              cout<<"a Producer put a thing "<<endl;
       ReleaseSemaphore(full,
1,NULL);         //V(full)
}
 
void Consumer(void*)
{
       cout
<<"a Consumer come in"<<endl;
       WaitForSingleObject(full,INFINITE);
//P(full)
              cout<<"a Consumer get a thing"<<endl;
       ReleaseSemaphore(empty,
1,NULL);    //V(empty)
}
 
int _tmain(int argc, _TCHAR* argv[])
{
       full 
= CreateSemaphore(NULL,0,1,"full");
       empty 
= CreateSemaphore(NULL,1,1,"emtpy");
 
       srand(time(NULL));
       
while(1)
       {
              Sleep(
500);
              
switch(rand()%2)
              {
              
case 0:
                     _beginthread(Producer,
0,NULL);
                     
break;
              
case 1:
                     _beginthread(Consumer,
0,NULL);
                     
break;
              
default:
                     
break;
              }
       }
       
return 0;
}
 
上面的代码可能把老师搞蒙了,简简单单的PV操作竟被做成100行,当时我没有请教过人,也没有问过她的老师对不对,是否有更简单的方法,但因为那不是我的作业,于是就不了了之了。
 
总评:虽然能实现PV操作,但代码根本不能重用,与其说是PV操作,不如说是专为消费者生产者的PV操作。
 
VC++动态链接库编程之基础慨念:【上一篇】
VC++中查找/替换对话框的使用:【下一篇】
【相关文章】
  • PVS
  • 关于SNMPV3协议开发
  • 使用migratepv移植系统及镜像
  • FreeBSD6.1 +CARP+IPVS双机热备份负载均衡
  • [转]/proc/sys/net/ipv4/ 参数
  • 推荐一本IPV6方面的好书--ipv6技术揭秘
  • 专用虚拟局域网(PVLAN)技术与应用
  • Ubuntu与PVO
  • PV加入rootvg做mirror出问题
  • UNIX.AIX.文件系统.存储管理.逻辑卷管理.PV.VG.PP.LV.LP
  • 【随机文章】
  • 中国如何引进CMM评估,促进软件产业发展
  • 目前国内常见的几款P2P网络电视软件简介
  • Password Keeper v6.3破解过程
  • o'reily ip routing memo part03
  • MDA
  • 德圳手机信号屏蔽器
  • CCNP Certification
  • 记录每个用户输入的命令
  • MyEclipse 5.0 M1 发布 支持Eclipse 3.2
  • color space desc
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.