• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            meet-dream

            boost asio 2:threading

            asio在handle級上提供了多線程保護鎖,命名為asio::strand,strand(繩、線之一股, 線, 繩, 串, 海濱, 河岸)顧名思義序列化,這個命名可能和asio的工作原理有關.asio的內部維護了一個隊列,當異步請求的狀態轉移為完成時,service會調用對應的線程對應的handle.如果在多線程方式下,可能有多個handle訪問共享變量.而strand把這一handle針對這一變量的的操作序列為不可分割的一段,這樣另外一段針對這一變量的的操作就必須等該操作完成后才能開始.這樣就起到了互斥信號的作用. 詳見下面的例子: #include #include #include #include #include class printer { public: printer(boost::asio::io_service& io) : strand_(io), timer1_(io, boost::posix_time::seconds(1)), timer2_(io, boost::posix_time::seconds(1)), count_(0) { timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this))); timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this))); } ~printer() { std::cout << "Final count is " << count_ << "\n"; } void print1() { if (count_ < 10) { std::cout << "Timer 1: " << count_ << "\n"; ++count_; timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1)); timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this))); } } void print2() { if (count_ < 10) { std::cout << "Timer 2: " << count_ << "\n"; ++count_; timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1)); timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this))); } } private: boost::asio::strand strand_; boost::asio::deadline_timer timer1_; boost::asio::deadline_timer timer2_; int count_; }; int main() { //啟動兩個線程 //asio的每個線程必須調用io_service::run,這個有點類似com的coinitlize boost::asio::io_service io; printer p(io); boost::thread t(boost::bind(&boost::asio::io_service::run, &io)); io.run(); t.join(); return 0; } 這個例子的最大不同就是 timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this))); 這一行.我們知道 timer1_.async_wait(boost::bind(&printer::print1, this)); 啟動了一個時鐘請求,而print1和print2函數訪問了共享變量cout_,而要互斥地訪問該變量,我們只需要用strand把該這兩個handle包裝一下(wrap).這樣一來兩個線程就可以安全的在屏幕輸出各自的信息(當然,這樣一來,輸出的信息時間的嚴格性就無法保證).如果去掉該wrap,我們將看到屏幕的輸出是無序的,而且變量的值也會出現變小的現象. asio的strand當然可以用在自己的函數內部,同時它也是跨平臺的互斥量噢.

            posted on 2007-05-25 14:06 meet-dream 閱讀(1556) 評論(2)  編輯 收藏 引用 所屬分類: boost library

            評論

            # re: boost asio 2:threading 2007-05-25 17:41 pass86

            亂碼顯示源代碼  回復  更多評論   

            # re: boost asio 2:threading 2007-05-25 17:53 ngaut

            不錯,建議用插入代碼的方式來排版代碼,會漂亮些^_^  回復  更多評論   

            A狠狠久久蜜臀婷色中文网| 久久久无码精品午夜| 99久久99久久| 九九热久久免费视频| 久久精品视频一| 国产精品免费久久久久影院 | 欧美777精品久久久久网| 午夜精品久久久久9999高清| 久久天天躁狠狠躁夜夜96流白浆| 久久精品国产精品亚洲艾草网美妙| 18岁日韩内射颜射午夜久久成人 | 一本色道久久88—综合亚洲精品 | 久久99热这里只频精品6| 99久久国语露脸精品国产| 久久久国产视频| 久久久久噜噜噜亚洲熟女综合 | 久久e热在这里只有国产中文精品99| 中文字幕乱码人妻无码久久| 久久婷婷五月综合成人D啪| 国内精品久久久久| 久久亚洲精品中文字幕| 无码任你躁久久久久久老妇App| 久久久无码精品亚洲日韩软件| 99久久国产综合精品网成人影院| 国内精品久久久久影院优| 综合人妻久久一区二区精品| 国产精品久久久久久五月尺| 一本一道久久a久久精品综合 | 国产高潮国产高潮久久久91| 久久99精品国产麻豆宅宅| 国内精品久久久久伊人av| 久久久久久九九99精品| 久久综合88熟人妻| 99久久精品国产麻豆| 狠狠色丁香婷婷综合久久来| 精品九九久久国内精品| 久久亚洲国产中v天仙www| 国产AⅤ精品一区二区三区久久| 国产L精品国产亚洲区久久| 久久本道久久综合伊人| 久久亚洲中文字幕精品一区四|