最近导师让做系统平台,其中要求函数能异步参数执行。比如,函数void Func(int a, int b);它的参数a和参数b是通过网络传递过来,但不是同时传递过来;函数Func需要等这两个参数都到来才执行。这一想法是我自己命名的“C++ 增强型方法”的最初设想。下面是我实现的一个雏形,其中参数必须是整型变量。
//Method.h
#include <vector>
using namespace std;
class Param
{
public:
Param(){}
Param(int mode, int value, const char* asyninfo);
public:
int mode;//mode == 0 means synchronization, mode == 1 means asynchronization
int value;
const char* asyninfo;
void* socket;
};
class Method
{
public:
Method();
~Method();
void SetParam(int index, int mode, int value, const char* asyninfo);//index参数序号
void Start();
void Stop();
public:
virtual void MethodContent();
void AddParam(Param param);
void WaitParam(int index, int millseconds);
void SendParam(const char* targetinfo, int value);
public:
static void* _MethodContent(void* arg);
void* pMethodName;
static void* context;
// void* socket;
vector<Param> inputParams;
};
其中定义了两个类,一个是参数类,一个是方法类
//Method.cpp
#include "Method.h"
#include <zhelpers.h>
#include <pthread.h>
#include <stdio.h>
Param::Param(int mode, int value, const char* asyninfo){
this->mode = mode;
this->value = value;
this->asyninfo = asyninfo;
}
void* Method::context =zmq_ctx_new();
Method::Method()
{
}
Method::~Method()
{
for(int i = 0; i < inputParams.size(); i++)
zmq_close(inputParams[i].socket);
zmq_ctx_destroy(context);
}
void Method::SetParam(int index, int mode, int value, const char* asyninfo)//index参数序号
{
inputParams[index].mode = mode;
inputParams[index].value = value;
inputParams[index].asyninfo = asyninfo;
}
void Method::WaitParam(int index, int millseconds)
{
zmq_pollitem_t items[]=
{
{inputParams[index].socket, 0, ZMQ_POLLIN, 0},
};
if (millseconds == -1)
{
while(1)
{
zmq_poll(items, 1, -1);
if(items[0].revents & ZMQ_POLLIN)
{
char* recv_msg = s_recv(inputParams[index].socket);
printf("received: %s\n", recv_msg);
sscanf(recv_msg, "%d", &(inputParams[index].value));
break;
}
}
}
else if(millseconds == 0)
{
zmq_poll(items, 1, 0);
if(items[0].revents & ZMQ_POLLIN)
{
char* recv_msg = s_recv(inputParams[index].socket);
printf("received: %s\n", recv_msg);
sscanf(recv_msg, "%d", &(inputParams[index].value));
}
}
else if(millseconds > 0)
{
zmq_poll(items, 1, millseconds);
if(items[0].revents & ZMQ_POLLIN)
{
char* recv_msg = s_recv(inputParams[index].socket);
printf("received: %s\n", recv_msg);
sscanf(recv_msg, "%d", &(inputParams[index].value));
}
}
else
{
printf("Method::WaitParam failed\n");
}
}
void Method::SendParam(const char* targetinfo, int value)
{
char msg[256];sprintf(msg, "%d", value);
void* send_socket = zmq_socket (context, ZMQ_DEALER);
int ret = zmq_connect(send_socket, targetinfo);
assert(ret == 0 && "sending has failed!\n");
s_send(send_socket, msg);
zmq_close(send_socket);
}
void Method::AddParam(Param param)
{
param.socket = zmq_socket(context, ZMQ_DEALER);
int ret = zmq_bind (param.socket, param.asyninfo);
assert(ret == 0 && "AddParam has failed!");
inputParams.push_back(param);
}
void Method::MethodContent()
{
}
void* Method::_MethodContent(void* arg)
{
Method* This = (Method*)arg;
This->MethodContent();
return arg;
}
void Method::Start()
{
pMethodName = new pthread_t;
pthread_create((pthread_t*)pMethodName, NULL, _MethodContent, this);
}
void Method::Stop()
{
pthread_cancel(*(pthread_t*)pMethodName);
delete (pthread_t*)pMethodName;
}
上面是实现文件。
//testMethod.cpp
#include "Method.h"
#include <stdio.h>
#include <vector>
using namespace std;
//-------使用实例-----------
//从Method类继承,然后实现两个部分:(1)在构造函数中添加参数(2)在MethodContent()中添加自己的代码
class Func : public Method
{
public:
Func(){
Param param1(0, 0, "inproc://param1");
AddParam(param1);
Param param2(0, 0, "inproc://param2");
AddParam(param2);
}
~Func(){}
void MethodContent()
{
int result;
WaitParam(0, -1);
WaitParam(1, -1);
result = inputParams[0].value + inputParams[1].value;
printf("var1 + var2 = %d\n", result);
}
};
void main()
{
Func method;
method.SetParam(0, 1, 1, "");
method.SetParam(1, 1, 1, "");
method.Start();
method.SendParam("inproc://param1", 100);
method.SendParam("inproc://param2", 2);
method.Stop();
while(1);
}
测试结果:
总结:它通过Start启动方法,然后设置方法参数属性,再通过外边发送参数给它,最后它利用收到的参数计算出两个参数的和并输出。