REACT-CPP是利用C++11的lambda表达式在文件描述符或定时器激活的时候进行通知的事件循环库。在它内部实质是对libev库的包装,因此也依赖于该库。
在一个典型的应用程序中,你创建了一个mailoop类的实例,然后注册你想要查看的filedescriptors,注册事件处理程序和计时器:
#iclude <reactcpp.h>#iclude <uistd.h>#iclude <iostream>/** * Mai applicatio procedure * @retur it */it mai(){ // create a evet loop React::MaiLoop loop; // set a timer to stop the applicatio after five secods loop.oTimeout(5.0, []() { // report that the timer expired std::cout << "timer expired" << std::edl; // stop the applicatio exit(0); }); // we'd like to be otified whe iput is available o stdi loop.oReadable(STDIN_FILENO, []() -> bool { // read iput std::strig buffer; std::ci >> buffer; // show what we read std::cout << buffer << std::edl; // retur true, so that we also retur future read evets retur true; }); // hadler whe cotrol+c is pressed loop.oSigal(SIGINT, []() -> bool { // report that we got a sigal std::cout << "cotrol+c detected" << std::edl; // stop the applicatio exit(0); // although this code is ureachable, we retur false because // we're o loger iterested i future SIGINT sigals retur false; }); // ru the evet loop loop.ru(); // doe retur 0;}









评论