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的更多相关文章

  1. jQuery1.9.1源码分析--Events模块

    var rformElems = /^(?:input|select|textarea)$/i, rkeyEvent = /^key/, rmouseEvent = /^(?:mouse|contex ...

  2. Using the Task Parallel Library (TPL) for Events

    Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...

  3. Oracle 11g trace events

    oracle的events,是我们在做自己的软件系统时可以借鉴的 Oracle 11g trace eventsORA-10001: control file crash event1ORA-1000 ...

  4. 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 ...

  5. NS Simulation: Scheduling Events (examples inside)

    NS Simulation: Scheduling Events Simulation time A similation system (such as NS) must have a built- ...

  6. [React & Testing] Simulate Event testing

    Here we want to test a toggle button component, when the button was click, state should change, styl ...

  7. WPF- 模拟触发Touch Events

    原文:WPF- 模拟触发Touch Events 基于API: [DllImport("User32.dll")] public static extern bool Initia ...

  8. ABP(现代ASP.NET样板开发框架)系列之14、ABP领域层——领域事件(Domain events)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之14.ABP领域层——领域事件(Domain events) ABP是“ASP.NET Boilerplate P ...

  9. Node.js:events事件模块

    Nodejs的大部分核心API都是基于异步事件驱动设计的,所有可以分发事件的对象都是EventEmitter类的实例. 大家知道,由于nodejs是单线程运行的,所以nodejs需要借助事件轮询,不断 ...

随机推荐

  1. json 模块 与 pickle 模块

    1 import json 3 dic={'name':'alvin','age':23,'sex':'male'} 4 print(type(dic))#<class 'dict'> 6 ...

  2. vee-validate表单验证组件

    vee-validate是VUE的基于模板的验证框架,允许您验证输入并显示错误 安装 npm i vee-validate --save 引入 import Vue from 'vue'; impor ...

  3. Web服务常见问题

    1. 资源无法找到 ,用58225端口发布服务的时候,没法找到 那么就换一个端口试试.http://localhost:58272/ 2. could not load file or assembl ...

  4. Nginx 配置反向代理后,页面中取绝对URL地址的问题显示代理端口

    本文有V型知识库提供 upstream tomcat {    server 127.0.0.1:82;} location / {     proxy_pass http://tomcat;} 如上 ...

  5. WeUI0.6.0有一个Calendar在浏览器下无法直接打开问题

    主要原因是判断浏览器不支持导致.修改isPopover相关部分代码即可.

  6. Javaweb学习笔记——(九)——————Servlet的进入,及ServletConfig、GenericServlet、HttpServlet、ServletContext、获取类路径资源

    Servlet1.什么是Servlet? *Servlet是Javaweb三大组件之一(Servlet,Filter,Listener) *Servlet是用来处理客户端请求的动态资源 *Servle ...

  7. C#--IEnumerable 与 IEnumerator 的区别

    一. IEnumerator 解释:它是一个的集合访问器,使用foreach语句遍历集合或数组时,就是调用 Current.MoveNext()的结果. // 定义如下public interface ...

  8. springboot11-security02FromDB 权限管理(用户信息和角色信息保存在数据库)

    <h4>场景</h4> <h4>代码</h4> springboot+springsecurity+mysql(jpa)实现: 1.pom依赖: < ...

  9. c# 适配器批量修改

    DataTable dt; //在方法外部申明数据表SqlDataAdapter adapter; //在方法外部申明数据适配器 查询方法adapter = new SqlDataAdapter(Sq ...

  10. Coursera, Deep Learning 5, Sequence Models, week1 Recurrent Neural Networks

    有哪些sequence model Notation: RNN - Recurrent Neural Network 传统NN 在解决sequence input 时有什么问题? RNN就没有上面的问 ...