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需要借助事件轮询,不断 ...
随机推荐
- Chrome刷新缓存
Ctrl+Shift+Del 清除Google浏览器缓存的快捷键 Ctrl+Shift+R 重新加载当前网页而不使用缓存内容
- 魔改版BBR
魔改版bbr加速: wget -N --no-check-certificate "https://raw.githubusercontent.com/chiakge/Linux-NetSp ...
- Postman 安装及使用入门教程 (谷歌浏览器插件版)
postman 在 谷歌浏览器中插件版 http://www.cnblogs.com/mafly/p/postman.html Postman 4.1.2 下载地址: http://files.cnb ...
- udp_client函数
#include <netdb.h> #include <stdlib.h> #include <string.h> #include <sys/socket ...
- 【SRM-07 D】天才麻将少女KPM
Description 天才麻将少女KPM立志要在日麻界闯出一番名堂.KPM上周叒打了n场麻将,但她这次又没控分,而且因为是全市参与的麻将大赛,所以她的名次范围是0..10^5.名次可能等于0是因为K ...
- Kali Linux之使用SET快捷生成钓鱼网站方法
SET (Social Engineering Tools) 1.使用命令:setoolkit 会显示工具菜单 2.输入1 ,选择菜单中的Social-Engineering Attacks (社会工 ...
- Embedded training,嵌入式训练
一旦初始的模型集被创建后, HERest使用整个训练集来执行"嵌入式训练(embedded training)",HERest将对全部HMM音素集模型执行一次Baum-Welch, ...
- Tomcat多应用启动报错:org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [].
Loaded org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller$RunnableRemove from .M22/lib/tomca ...
- 表单相关标签之input标签
用于搜集用户信息. <input type="text" name="fname" /> 标签属性 type 规定 input 元素的类型.输入字段 ...
- python 07
1.文件操作: f=open(...) 是由操作系统打开文件,那么如果我们没有为open指定编码,那么打开文件的默认编码很明显是操作系统说了算了 操作系统会用自己的默认编码去打开文件,在windows ...