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. 1、PHP入门二维数组与循环

    <?php $two=array(array(2,3),1=>array(1,2,3),2=>array(4,5,6)); echo $two[1][0];//输出1 echo $t ...

  2. C++获取MAC与IP

    #include <Nb30.h> #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"netapi ...

  3. Docker 添加环境系统文件配置

    在 docker 启动文件添加默认环境系统配置 " /etc/default/docker ": 添加  Environment File 配置: # vi /usr/lib/sy ...

  4. C++ 初始化函数

    初始化函数:OnInitDialog()     在这个位置添加初始化代码

  5. VMWare14 安装Mac OS系统(图解)

    ★ 背景 瞅了瞅自己干瘪的钱包,没忍心入手期待已久的 macPro,只好在虚拟机里玩一下 mac好了,等以后钱包傲气的时候再来个真实的. 安装环境: windows10 VMWare14.2 一.准备 ...

  6. Hbase思维导图之架构

  7. Django REST framework 第二章 Request and Response

    此章节开始真正的撰写REST framework的核心代码,介绍一系列必要的建立设计 Request Objects REST framework介绍了一个Request对象用来扩展常规的HttpRe ...

  8. Shiro入门 - 通过自定义Realm连数数据库进行认证(md5+salt形式)

    shiro-realm-md5.ini [main] #定义凭证匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCrede ...

  9. 连接字符串配置在App.config中

    <?xml version="1.0" encoding="utf-8"?> <configuration> <connectio ...

  10. WC2019 20天训练

    Day -1 2019.1.2 初步计划: 0x60 图论 std 洛谷提高剩余练习 NOIP2018遗留题解 洛谷省选基础练习 数学: 1.数论 2.组合数学(练习:莫比乌斯反演) 3.概率(练习: ...