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. flask 文件转为pdf并添加二维码

    背景: 宝安区需求,企业会下载表格,打印后填报.填报后收上表格,统一录入PDA.因为某台PDA只能录某个地方的表格,所以他们希望纸质表上有个二维码,扫描出现填报公司的一些信息,以及统计(好像是这样,没 ...

  2. 【由浅入深理解java集合】(五)——集合 Map

    前面已经介绍完了Collection接口下的集合实现类,今天我们来介绍Map接口下的两个重要的集合实现类HashMap,TreeMap.关于Map的一些通用介绍,可以参考第一篇文章.由于Map与Lis ...

  3. 【八】虚拟机工具 01 jps命令详解

    JPS 名称: jps - Java Virtual Machine Process Status Tool 命令用法: jps [options] [hostid] options:命令选项,用来对 ...

  4. 按科室统计 2.181222版本 关联查询join 不对

    SQL: select t0.deptName, t0.deptId, t0.startTime, t0.endTime, IFNULL(t0.num,) as num0, IFNULL(t1.num ...

  5. 26. SpringBoot 初识缓存及 SimpleCacheConfiguration源码解析

    1.引入一下starter: web.cache.Mybatis.MySQL @MapperScan("com.everjiankang.cache.dao") @SpringBo ...

  6. Weex Ui - Weex Conf 2018 干货分享

    本文是2018年 Weex Conf 中议题<Weex + Ui>的内容文档整理,主要给大家介绍飞猪 Weex 技术体系从无到有的过程,包括 Weex Ui 组件库的开发和发展,重点分享在 ...

  7. mysql插入记录INSERT与多表更新

    1.第一种:INSERT [INTO] tbl_name[ (col_name, ... ) ]  {VALUES | VALUE}({expr |default}, ... ), (...), .. ...

  8. sql-leetcode Consecutive Numbers

    开始想 用 group 把Num都聚在一起 -- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.--它的作用是通过一定的规则将一个数据集划分成若干个小 ...

  9. core.net 创建coreclass 项目出现一些问题

    错误如下: Project CoreTest does not have a lock file. Please run "dotnet restore" to generate ...

  10. C# CancellationTokenSource 终止线程 CancellationTokenSource实现对超时任务的取消

    C# 使用 CancellationTokenSource 终止线程 使用CancellationTokenSource对象需要与Task对象进行配合使用,Task会对当前运行的状态进行控制(这个不用 ...