simulate events
windows system maintains a msg queue, and any process that supports msg will create an thread that have a loop which checks its own msg queue;
we can set a callback for it through define an event.

// file Eventt.h
#include <map>
#include <functional>
using namespace std; template<class T1, class T2>
class Eventt
{
public:
typedef void FunctionA(T1, T2);
Eventt() :eventID(0), softwareID(0){}
template<class T3> int addHandler(T3 t3)
{
m_handlers.emplace(eventID, t3);
return eventID++;
} void operator()(T1 t1, T2 t2)
{
for (auto i : m_handlers) i.second(t1, t2);
} private:
map<int, function<FunctionA> > m_handlers;
int eventID;
int softwareID;
};
#include "Eventt.h"
#include <iostream>
#include <queue>
#include <thread>
#include <stdlib.h>
#include <windows.h>
using namespace std; // to simulate msg and event machinism used in Windows struct Messg
{
enum SoftType{ NUL, A, B };
int a, b, Type;
Messg(int aa, int bb, int cc) :a(aa), b(bb), Type(cc){}
}; struct
{
queue<pair<int, int>> m_softA, m_softB;
queue<Messg> m_system;
} Computr; void fuctionA(int a, int b)
{
cout << "software A : " << a*b << endl;
} void fuctionB(int a, int b)
{
cout << "software B : " << a * b <<"\t\t" << a+b << endl;
} void FunctionCC(void)
{
int a, b, c;
while (1)
{
Sleep(1500);
a = rand() % 100;
b = rand() % 100;
c = rand() % 3;
Computr.m_system.push(Messg(a, b, c));
}
} void FunctionAA(void)
{
Eventt<int, int> ea;
// register callback
ea.addHandler(fuctionA);
while (1)
{
Sleep(1000);
while (!Computr.m_softA.empty())
{
pair<int, int> msg(Computr.m_softA.front());
ea(msg.first, msg.second);
Computr.m_softA.pop();
}
}
} void FunctionBB(void)
{
Eventt<int, int> ea;
ea.addHandler(fuctionB);
while (1)
{
Sleep(1000);
while (!Computr.m_softB.empty())
{
pair<int, int> msg(Computr.m_softB.front());
ea(msg.first, msg.second);
Computr.m_softB.pop();
}
}
} int main() // the function main is like a system
{
thread softA(FunctionAA), softB(FunctionBB), softC(FunctionCC);
while (1)
{
Sleep(700);
if (!Computr.m_system.empty())
{
Messg temp = Computr.m_system.front();
pair<int, int> tempmsg(temp.a, temp.b);
switch (temp.Type)
{
case Messg::NUL:Computr.m_softA.push(tempmsg);
Computr.m_softB.push(tempmsg);
break;
case Messg::A:Computr.m_softA.push(tempmsg); // send mesg to softA
break;
case Messg::B:Computr.m_softB.push(tempmsg); // send mesg to softB
break;
default: ;
}
Computr.m_system.pop();
}
}
}
simulate events的更多相关文章
- jQuery1.9.1源码分析--Events模块
var rformElems = /^(?:input|select|textarea)$/i, rkeyEvent = /^key/, rmouseEvent = /^(?:mouse|contex ...
- Using the Task Parallel Library (TPL) for Events
Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...
- Oracle 11g trace events
oracle的events,是我们在做自己的软件系统时可以借鉴的 Oracle 11g trace eventsORA-10001: control file crash event1ORA-1000 ...
- Automate Screen or Button Taps via Tasker : Simulating keypress events
When using Tasker, sometimes we want to do some automation on screen e.g. screen or button taps. At ...
- NS Simulation: Scheduling Events (examples inside)
NS Simulation: Scheduling Events Simulation time A similation system (such as NS) must have a built- ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- WPF- 模拟触发Touch Events
原文:WPF- 模拟触发Touch Events 基于API: [DllImport("User32.dll")] public static extern bool Initia ...
- ABP(现代ASP.NET样板开发框架)系列之14、ABP领域层——领域事件(Domain events)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之14.ABP领域层——领域事件(Domain events) ABP是“ASP.NET Boilerplate P ...
- Node.js:events事件模块
Nodejs的大部分核心API都是基于异步事件驱动设计的,所有可以分发事件的对象都是EventEmitter类的实例. 大家知道,由于nodejs是单线程运行的,所以nodejs需要借助事件轮询,不断 ...
随机推荐
- int、bool和str
int bit_length 返回以二进制表示的最短长度 print(int.bit_length(10)) 结果 4 Process finished with exit code 0 int() ...
- z-index设置后导致遮罩层显示跳动问题
如图,1,3为top,bottom div,2为iscroll,4为遮罩层,如果1设置z-index后,不设置遮挡不住2,遮罩层4弹出会卡顿,既不设置z-index,又能遮挡iscroll的办法是在h ...
- 【二】Spring Cloud 入门
官网 版本号: SpringCloud中文网:https://springcloud.cc SpringCloud中文社区:http://springcloud.cn 以下代码就是Maven父子工程, ...
- seleniums私房菜系列一 ---- selenium简介
一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...
- hasnMap的基本操作 源码(三)
一.初始化: hashMap有四种初始化方式: public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity ...
- web渗透测试基本步骤
基本常见步骤: 一 .信息收集 要检测一个站首先应先收集信息如whois信息.网站真实IP.旁注.C段网站.服务器系统版本.容器版本.程序版本.数据库类型.二级域名.防火墙.维护者信息有哪些等等 ...
- cpp #,##
#define语句中的#是把参数字符串化,##是连接两个参数成为一个整体. #define FACTORY_REF(name) { #name, Make##name } 中#name就是将传入的na ...
- Flume思维导图
- 集成JUnit测试错误java.lang.IllegalStateException: Failed to load ApplicationContext
1 详细错误信息 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.t ...
- 前端与后端的数据交互(jquery ajax+python flask)
前端与后端的数据交互,最常用的就是GET.POST,比较常用的用法是:提交表单数据到后端,后端返回json 前端的数据发送与接收 1)提交表单数据 2)提交JSON数据 后端的数据接收与响应 1)接收 ...