Boost Asio总结(7)class strand
生活随笔
收集整理的這篇文章主要介紹了
Boost Asio总结(7)class strand
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
strand 保證異步代碼在多線程環(huán)境中正確執(zhí)行,無須使用互斥量,好比一組handler的鎖,加入線程保護,保證handler不會存在線程并發(fā)訪問。
多個線程使用io_service需要strand保證線程安全。
class strand { //返回contextexecution_context& context() const BOOST_ASIO_NOEXCEPT{return executor_.context();}//要求異步執(zhí)行一個handlertemplate <typename Function, typename Allocator>void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const{detail::strand_executor_service::dispatch(impl_,executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);}//要求異步執(zhí)行一個handler/// Request the strand to invoke the given function object. template <typename Function, typename Allocator>void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const{detail::strand_executor_service::post(impl_,executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);}bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT{return detail::strand_executor_service::running_in_this_thread(impl_);}總結
以上是生活随笔為你收集整理的Boost Asio总结(7)class strand的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Boost Asio总结(5)class
- 下一篇: Boost Asio总结(9)数据缓冲区