int main( int argc, char **argv ) { // google::InitGoogleLogging(argv[0]); LOG(INFO) << "main thread " << THIS_THREAD_ID << " running..."; // 如果没有,io_service 执行完了队列中所有缓存的job就退出,在getchar()之前terminating boost::asio::io_service::work io_work( std::ref(g_io_service) ); boost::thread_group thrgrp; for (int i = 0; i < 5; ++i) thrgrp.create_thread( run_io_service ); SLEEP_SECONDS(1); g_io_service.dispatch( std::bind(job_func, 1) ); getchar(); LOG(INFO) << "Trying to stop io_service..."; // NOTE!!! 如果job_func里用dispatch就停不下来,嵌套 g_io_service.stop(); thrgrp.join_all(); return 0; }
~http_sync_server() { error_code ec; ios_.dispatch( [&]{ acceptor_.close(ec); }); thread_.join(); }
/// Destructor. ~server() { work_ = boost::none; ios_.dispatch([&] { error_code ec; acceptor_.close(ec); }); for(auto& t : thread_) t.join(); }
void wlan_event_handler(const WLAN_NOTIFICATION_DATA& nd, boost::asio::io_service& ios, link_sap::link_sap* ls) { std::cout << "wlan notification[" << nd.NotificationCode << "]\n"; if (nd.NotificationSource != WLAN_NOTIFICATION_SOURCE_ACM) return; switch (nd.NotificationCode) { case wlan_notification_acm_connection_complete: { WLAN_CONNECTION_NOTIFICATION_DATA* cnd = reinterpret_cast<WLAN_CONNECTION_NOTIFICATION_DATA*>(nd.pData); if (cnd->wlanReasonCode == WLAN_REASON_CODE_SUCCESS) { interface* it = new if_802_11(if_id(&nd.InterfaceGuid)); it->up(true); MIB_IF_ROW2 it_info = link_sap::win32::get_interface_info(nd.InterfaceGuid); it->link_addr(odtone::mih::mac_addr(it_info.PhysicalAddress, it_info.PhysicalAddressLength)); ios.dispatch(boost::bind(&link_sap::link_sap::update, ls, it)); } } break; case wlan_notification_acm_disconnected: { interface* it = new if_802_11(if_id(&nd.InterfaceGuid)); it->up(false); MIB_IF_ROW2 it_info = link_sap::win32::get_interface_info(nd.InterfaceGuid); it->link_addr(odtone::mih::mac_addr(it_info.PhysicalAddress, it_info.PhysicalAddressLength)); ios.dispatch(boost::bind(&link_sap::link_sap::update, ls, it)); } } }
// 将上面的m_strand换成m_service后, void service_print1() { // PRINT_DEBUG("Enter print1"); m_service.dispatch(boost::bind(print, 1)); // PRINT_DEBUG("Exit print1"); }