• <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級上提供了多線程保護(hù)鎖,命名為asio::strand,strand(繩、線之一股, 線, 繩, 串, 海濱, 河岸)顧名思義序列化,這個命名可能和asio的工作原理有關(guān).asio的內(nèi)部維護(hù)了一個隊(duì)列,當(dāng)異步請求的狀態(tài)轉(zhuǎn)移為完成時,service會調(diào)用對應(yīng)的線程對應(yīng)的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的每個線程必須調(diào)用io_service::run,這個有點(diǎn)類似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函數(shù)訪問了共享變量cout_,而要互斥地訪問該變量,我們只需要用strand把該這兩個handle包裝一下(wrap).這樣一來兩個線程就可以安全的在屏幕輸出各自的信息(當(dāng)然,這樣一來,輸出的信息時間的嚴(yán)格性就無法保證).如果去掉該wrap,我們將看到屏幕的輸出是無序的,而且變量的值也會出現(xiàn)變小的現(xiàn)象. asio的strand當(dāng)然可以用在自己的函數(shù)內(nèi)部,同時它也是跨平臺的互斥量噢.

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

            評論

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

            亂碼顯示源代碼  回復(fù)  更多評論   

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

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

            久久婷婷五月综合国产尤物app | 怡红院日本一道日本久久 | 性做久久久久久久久久久| 国产免费久久精品丫丫| 久久综合视频网站| 久久综合色区| 久久九九久精品国产免费直播| 午夜精品久久久久久久久| av无码久久久久不卡免费网站| 伊人久久综合热线大杳蕉下载| 中文无码久久精品| 久久精品九九亚洲精品天堂| 亚洲欧洲精品成人久久奇米网| 久久精品人人做人人爽电影蜜月| 国产精品亚洲综合专区片高清久久久 | 久久影视综合亚洲| 国产亚洲精久久久久久无码| 欧美午夜A∨大片久久| 人妻精品久久无码专区精东影业| 久久国产精品国产自线拍免费| 欧美日韩精品久久久久| 久久精品国产久精国产思思| 久久久这里只有精品加勒比| 久久亚洲国产中v天仙www| 久久国产亚洲精品无码| 欧美成a人片免费看久久| 青草影院天堂男人久久| 久久久久久亚洲Av无码精品专口| 久久亚洲电影| 久久AⅤ人妻少妇嫩草影院| 久久精品国产免费一区| 久久久久亚洲Av无码专| 久久免费的精品国产V∧| 99久久国产亚洲综合精品| 麻豆久久久9性大片| 久久精品极品盛宴观看| 看全色黄大色大片免费久久久 | 久久99精品国产自在现线小黄鸭| 久久强奷乱码老熟女网站| 中文字幕精品久久| 无码人妻少妇久久中文字幕|