当前位置:
首页 >
boost asio
发布时间:2025/3/21
35
豆豆
生活随笔
收集整理的这篇文章主要介绍了
boost asio
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1) 不要多线程同时对一个socket 进行asio::async_write, 也不要在一个线程内对一个socket多次async_write。
理由:async_write是个异步操作,数据较大时, 会分多次调用async_write_some发送出去。 因此即便是一个线程内的多个async_write, 其async_write_some也可能交叉乱序了。
2)read_handler, write_handler、timeout_handler的参数都应该是const类型的。虽然可以定义成非const型参数、修改也不会有编译错误, 但是修改不会生效。
3)各种handler应该使用堆内存、全局变量、或者shared_from_this(), 或者类变量的成员。 不能使用栈变量; 使用栈变量的话,需要使用boost::ref进行修饰。
boost::asio::deadline_timer timer(m_ioservice); timer.expires_from_now(boost::posix_time::seconds(0)); timer.async_wait(boost::bind(&RecIO::timeout_handler, shared_from_this(), <span style="color:#FF0000;">boost::ref(timer)</span>, int(timer_queue_type), boost::asio::placeholders::error));4)boost_auto宏, 自动推导变量类型。 <boost/typeof.hpp>
5)boost poperty_tree解析xml
http://www.oschina.net/code/snippet_126720_49526)boost::call_once, 保证只被执行一次。 google protobuf中也有类似实现
http://www.oschina.net/code/snippet_54334_870
总结
以上是生活随笔为你收集整理的boost asio的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: hashmap::begin() 坑
- 下一篇: 高性能服务器本质论