最近在写一个linux程序,没有界面的那种,中间牵扯的东西有点多,不能像简单的那种一个main函数里面搞定。里面的循环有点多,写成线程里面for循环,将来维护会死的。
想用Qt,但是centos的环境没有GUI,连我开发都是用的vs2017。
就看到了libevent,研究了一下,发现基本可以满足我的需要
Qt的写法
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
libevent的写法
struct event_base* t_base = NULL;
t_base = event_base_new();
ZhListernerManager t_AutoCallManager(t_base);
event_base_dispatch(t_base);
ZhListernerManager 里面添加一个定时器
m_pCheckTaskEvent = event_new(m_pEventbase, -1, EV_PERSIST | EV_TIMEOUT, s_time_out_cb, this);
timeval t_time_spc = { 2,0 };//这里每2s检查一次
event_add(m_pCheckTaskEvent, &t_time_spc);
添加tcpudpserver的方式类似
然后就基本达到类似效果了。