1 Star 0 Fork 92

xystar2012/handy

forked from yedf/handy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
safe-close.cc 674 Bytes
一键复制 编辑 原始数据 按行查看 历史
yedf 提交于 2016-03-21 11:43 . add more examples
#include <handy/handy.h>
using namespace handy;
int main(int argc, const char* argv[]) {
EventBase base;
Signal::signal(SIGINT, [&]{ base.exit(); });
TcpServerPtr svr = TcpServer::startServer(&base, "", 99);
exitif(svr == NULL, "start tcp server failed");
TcpConnPtr con = TcpConn::createConnection(&base, "localhost", 99);
std::thread th([con,&base](){
sleep(1);
info("thread want to close an connection");
base.safeCall([con](){ con->close(); }); //其他线程需要操作连接,应当通过safeCall把操作交给io线程来做
});
base.runAfter(1500, [&base](){base.exit();});
base.loop();
th.join();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/xystar2012/handy.git
git@gitee.com:xystar2012/handy.git
xystar2012
handy
handy
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385