boost asio
生活随笔
收集整理的這篇文章主要介紹了
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() 坑
- 下一篇: 高性能服务器本质论