• <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級(jí)上提供了多線程保護(hù)鎖,命名為asio::strand,strand(繩、線之一股, 線, 繩, 串, 海濱, 河岸)顧名思義序列化,這個(gè)命名可能和asio的工作原理有關(guān).asio的內(nèi)部維護(hù)了一個(gè)隊(duì)列,當(dāng)異步請(qǐng)求的狀態(tài)轉(zhuǎn)移為完成時(shí),service會(huì)調(diào)用對(duì)應(yīng)的線程對(duì)應(yīng)的handle.如果在多線程方式下,可能有多個(gè)handle訪問(wèn)共享變量.而strand把這一handle針對(duì)這一變量的的操作序列為不可分割的一段,這樣另外一段針對(duì)這一變量的的操作就必須等該操作完成后才能開始.這樣就起到了互斥信號(hào)的作用. 詳見(jiàn)下面的例子: #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() { //啟動(dòng)兩個(gè)線程 //asio的每個(gè)線程必須調(diào)用io_service::run,這個(gè)有點(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; } 這個(gè)例子的最大不同就是 timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this))); 這一行.我們知道 timer1_.async_wait(boost::bind(&printer::print1, this)); 啟動(dòng)了一個(gè)時(shí)鐘請(qǐng)求,而print1和print2函數(shù)訪問(wèn)了共享變量cout_,而要互斥地訪問(wèn)該變量,我們只需要用strand把該這兩個(gè)handle包裝一下(wrap).這樣一來(lái)兩個(gè)線程就可以安全的在屏幕輸出各自的信息(當(dāng)然,這樣一來(lái),輸出的信息時(shí)間的嚴(yán)格性就無(wú)法保證).如果去掉該wrap,我們將看到屏幕的輸出是無(wú)序的,而且變量的值也會(huì)出現(xiàn)變小的現(xiàn)象. asio的strand當(dāng)然可以用在自己的函數(shù)內(nèi)部,同時(shí)它也是跨平臺(tái)的互斥量噢.

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

            評(píng)論

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

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

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

            不錯(cuò),建議用插入代碼的方式來(lái)排版代碼,會(huì)漂亮些^_^  回復(fù)  更多評(píng)論   

            久久国产精品久久国产精品| 精品国产乱码久久久久久1区2区| 久久99国产精品久久久| 久久精品一本到99热免费| 很黄很污的网站久久mimi色| 久久久免费观成人影院| 精品久久久噜噜噜久久久 | 久久亚洲精品成人AV| 久久久久国产一级毛片高清板| 久久大香香蕉国产| 性高朝久久久久久久久久| 国产精品狼人久久久久影院| 色婷婷综合久久久中文字幕| 久久99精品久久久久久秒播| 亚洲国产另类久久久精品黑人 | 99久久精品无码一区二区毛片| 日韩精品久久无码人妻中文字幕| 久久久九九有精品国产| 国产精品一区二区久久国产| 久久丝袜精品中文字幕| 99国产精品久久| a级毛片无码兔费真人久久| 无码伊人66久久大杳蕉网站谷歌 | 久久精品亚洲精品国产色婷 | 日日躁夜夜躁狠狠久久AV| 青青热久久国产久精品| 国产成人精品久久亚洲高清不卡| 久久久久久无码Av成人影院| 久久精品国产男包| 亚洲中文字幕无码久久2017| 国产成人精品综合久久久久| 人妻少妇精品久久| 久久久久久极精品久久久| 激情久久久久久久久久| 久久精品免费一区二区三区| 国内精品人妻无码久久久影院| 人妻少妇久久中文字幕| 久久天天躁狠狠躁夜夜网站| 久久久久久毛片免费播放| 久久国产高潮流白浆免费观看| 69久久精品无码一区二区|