Mario同步到异步的类库
Mario 是一个让编写从同步到异步的类库,它的线程安全较大,易于使用。Mario 的最基本的想法就是为了减少人员的安排,降低成本和时间投入。但是有了这个类库,操作人员就可以抽出精力做别的事情了。所以 Mario 类库能够很轻易的解决你的问题,你只需要你自己的消息功能。
引擎类型:
-
memory,这种类型就是将数据缓冲存储器留在内存里。
-
file,这种类型就是能够在本地日志路径里创建做出一个 write2file。
示例代码:
#include "mario.h"
#include "consumer.h"
#include "mutexlock.h"
#include "port.h"
/**
* @brief The handler inherit from the Consumer::Handler
* The processMsg is pure virtual function, so you need implementation your own
* version
*/
class FileHandler : public mario::Consumer::Handler
{
public:
FileHandler() {};
virtual void processMsg(const std::string &item) {
log_info("consume data %s", item.data());
}
};
int main()
{
mario::Status s;
FileHandler *fh = new FileHandler();
/**
* @brief
*
* @param 1 is the thread number
* @param fh is the handler that you implement. It tell the mario how
* to consume the data
*
* @return
*/
mario::Mario *m = new mario::Mario(1, fh);
std::string item = "Put data in mario";
s = m->Put(item);
if (!s.ok()) {
log_err("Put error");
exit(-1);
}
delete m;
delete fh;
return 0;
}评论
