订阅数据端:
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "test.h"
//定义回调函数
int PrintfText(int a)
{
printf("Hello World!\n");
printf("Hello World!%d\n",a);
return a*10;
}
//实现函数回调
int main(int argc,char* argv[])
{
appRegisted(PrintfText);//将回调函数注册到发布端
int ret;
ret = keypoll();//这里触发执行,也可以主动上报。ret即可有值。
system("pause");
return 0;
}
上报数据端:
test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "test.h"
callfuction m_pCallback;
void appRegisted(callfuction pf)//订阅端将回调函数注册过来
{
m_pCallback = pf;
}
int keypoll(){
int re;
re = m_pCallback(5);
return re;
}
test.h
#pragma once
#include<iostream>
using namespace std;
typedef int (*callfuction)(int);
void appRegisted(callfuction pf);
int keypoll();
执行结果:
发布端发布了数据为5,ret为50