• <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>

            colorful

            zc qq:1337220912

             

            boost::asio::deadline_timer

            注意deadline_timer和socket一樣,都用 io_service作為構造函數的參數。也即,在其上進行異步操作,都將導致和io_service所包含的iocp相關聯。這同樣意味著在析構 io_service之前,必須析構關聯在這個io_service上的deadline_timer。

            一個deadline_timer只維護一個超時時間,一個deadline_timer不同時維持多個定時器。

            void wait();
            void
            wait(boost::system::error_code & ec);

            這是個同步等待函數,例如:

            boost::asio::io_service io;
            boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
            t.wait();
            由于不涉及到異步,該函數和io_service沒什么關系。這個函數在windows下的實現就只是簡單的Sleep。因此也就不存在cancel之說。

            如果t的expire時間已過,那么t.wait會立刻返回。

            例如如下代碼:

            boost::asio::io_service io; 
            boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
            t.wait();
            t.wait();

            第一個t.wait會等待5s才返回,第2個t.wait會立刻返回。

            wait函數本身沒有參數,不存在t.wait(seconds(5))的用法。

            可以在構造deadline_timer時指定時間。

            basic_deadline_timer(
            boost::asio::io_service & io_service);

            basic_deadline_timer(
            boost::asio::io_service & io_service,
            const time_type & expiry_time);

            basic_deadline_timer(
            boost::asio::io_service & io_service,
            const duration_type & expiry_time);

            注意后兩種的區別。以下2種用法是等價的:

            boost::asio::deadline_timer t(io, boost::posix_time::microsec_clock::universal_time()+boost::posix_time::seconds(5));

            boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));

            前者是絕對時間,后者是相對時間。

            除了在deadline_timer構造函數中指定時間,也可以使用如下2個函數指定時間:

            expires_at,expires_from_now。這兩個函數的區別是前者參數是絕對時間,后者是相對時間。例如:

            boost::asio::io_service io;

            boost::asio::deadline_timer t(io);

            t.expires_from_now(boost::posix_time::seconds(5));

            t.wait();

            注意這兩個函數除了設定下次超時時間之外,還有一個效果是取消前面所有的異步wait。詳情參看關于這兩個函數的詳細解釋。

            template<
                typename
            WaitHandler>
            void async_wait(
                WaitHandler handler);
            其中void handler(
            const boost::system::error_code& error // Result of operation.
            );

            注意這個error很重要,表明這個handler是因為超時被執行還是因為被cancel。
            符合2種情況之一,handler被執行:超時或者被cancel。
            這同時隱含的說明了除非io.stop被調用,否則handler一定會被執行。即便是被cancel。
            被cancel有多種方法,直接調用cancel或者調用expires_at,expires_from_now重新設置超時時間。
            void handle_wait(const boost::system::error_code& error,
            boost::asio::deadline_timer& t,int& count)
            {
            if(!error)
            {
            std::cout<< count<<"\n";
            if(count++<5)
            {
            t.expires_from_now(boost::posix_time::seconds(1));
            t.async_wait(boost::bind(handle_wait,boost::asio::placeholders::error,
            boost::ref(t),boost::ref(count)));
            }
            }
            }

            int main()
            {
            boost::asio::io_service io;
            boost::asio::deadline_timer t(io);
            size_t a = t.expires_from_now(boost::posix_time::seconds(1));
            int count = 0;
            t.async_wait(boost::bind(handle_wait,boost::asio::placeholders::error,
            boost::ref(t),boost::ref(count)));
            io.run();
            return 0;
            }

            deadline_timer的析構函數什么也不做,因此不會導致發出的async_wait被cancel。

            std::size_t cancel();

            std::size_t
            cancel(
                boost::system::error_code & ec);
            此函數調用會導致所有尚未返回的async_wait(handler)的handler被調用,同時error_code為boost::asio::error::operation_aborted。返回值是被cancel的timer數量。

             time_type expires_at() const;

            std::size_t
            expires_at(
            const time_type & expiry_time);

            std::size_t
            expires_at(
            const time_type & expiry_time,
            boost::system::error_code & ec);
            duration_type expires_from_now() const;

            std::size_t
            expires_from_now(
            const duration_type & expiry_time);

            std::size_t
            expires_from_now(
            const duration_type & expiry_time,
            boost::system::error_code & ec);
            以上2組函數用來設置新的超時時間,同時cancel所有未完成的async_wait操作。注意這兩個函數的返回值即為cancel的操作數量。
            考慮如下場景,我們有一個workerthread正在調用io_work.run();

            此時主線程向workerthread發出了一個異步調用,例如post(...),考慮到io_work.run很可能積壓了很多handlers沒有處理,或者某些handlers處理非常耗時,希望它在5s內必須返回。那么可以:

            void handle_wait(const boost::system::error_code& error,bool& Ret)
            {
                if(!error) Ret = false;
            }

            void handle_func(
                boost::shared_ptr<boost::asio::deadline_timer> t,
                boost::shared_ptr<boost::asio::io_service> io,
                int* v)
            {
                boost::asio::io_service::work work(*io);

                if(t->cancel()>0)
                {       
                    *v = 1;
                }
            }

            void func_delay_1_second()
            {
                boost::asio::io_service io;
                boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));
                t.wait();
            }

            bool sync_func(int& v,boost::asio::io_service& io_work)
            {
                boost::shared_ptr<boost::asio::io_service> io(new boost::asio::io_service);
                boost::shared_ptr<boost::asio::deadline_timer> t(new boost::asio::deadline_timer(*io));
                t->expires_from_now(boost::posix_time::seconds(5));
                bool ret = true;
                t->async_wait(boost::bind(handle_wait,boost::asio::placeholders::error,boost::ref(ret)));
                io_work.post(boost::bind(handle_func,t,io,&v));
                io->run();
                return ret;
            }

            int main()
            {
                boost::asio::io_service io_work;
                auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(io_work));
                boost::thread workthread(boost::bind(&boost::asio::io_service::run, &io_work));
                for(int i=0;i<3;++i) io_work.post(func_delay_1_second);

                int v = 0;
                bool ret = sync_func(v,io_work);
                if(ret) printf("v %d\n",v);

                work.reset();
                workthread.join();
                return 0;
            }

            上面代碼中如果先進入handle_wait,則表明超時,此時設置ret = false,然后io.run會退出,表明調用失敗,如果稍后進入handle_func,則t->cancel會返回0,也不做任何操作。雖然在 io.run退出時會釋放v,但由于handle_func不做任何操作因此也不會引起任何安全問題。如果handle_func先進入,則首先使用 work讓io.run不會退出,然后取消timer,并且設置,隨后work析構,io.run會退出。注意這里面的同步問題:如果先進入 handle_wait,隨后進入handle_func,那么handle_func中的t->cancel會返回0從而不做任何事。如果先進入 handle_func,隨后進入handle_wait,那么t->cancel或者返回0或者返回1,由于使用了work,io.run也不會 退出。注意這里的t和io都是shared_ptr的,否則因為如果handle_wait先返回,則io.run會立刻退出并析 構,handle_func中將使用懸空的t和io,將導致非法操作。注意這里的io必須是shared_ptr的,如果 boost::asio::io_service::work work(*io); 改成work(t->get_ioservice());則t是有效的,而t所索引的io_service已經無效了,同樣會導致非法操作。牢記 io_service的使用原則:必須首先析構所有索引的其他對象之后才能析構io_service。

            posted on 2012-03-30 16:39 多彩人生 閱讀(2266) 評論(1)  編輯 收藏 引用

            評論

            # re: boost::asio::deadline_timer[未登錄] 2012-11-27 17:22 lin

            請教下
            有沒有支持多個定時器的timer呢?  回復  更多評論   

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品美女久久久久AV福利 | 久久精品国产亚洲AV麻豆网站| 亚洲日本va午夜中文字幕久久 | 国产成人综合久久久久久| 久久国产精品视频| 久久久久久久精品妇女99| 99久久99这里只有免费费精品| 超级碰久久免费公开视频| 奇米影视7777久久精品人人爽| 久久99国产综合精品女同| 蜜桃麻豆www久久国产精品| 久久中文骚妇内射| 久久精品18| 人人狠狠综合久久亚洲88| 国产亚洲美女精品久久久2020| 色综合久久久久网| 久久久无码一区二区三区| 人妻系列无码专区久久五月天| 99久久国产热无码精品免费| 久久强奷乱码老熟女网站| 国产精品欧美亚洲韩国日本久久| 亚洲va久久久噜噜噜久久狠狠| 久久国产成人午夜aⅴ影院| 国产一区二区精品久久| 久久久久亚洲av综合波多野结衣| 精品久久久久久国产免费了| 亚洲国产精品婷婷久久| 99久久免费国产精精品| 午夜人妻久久久久久久久| 区久久AAA片69亚洲| 久久毛片一区二区| 最新久久免费视频| 亚洲国产精品无码久久久久久曰| 91久久精品电影| 精品无码久久久久久久动漫| 国产69精品久久久久99尤物| 国产精品成人无码久久久久久| 久久精品国产99久久香蕉| 久久久久亚洲AV无码专区桃色| 久久亚洲天堂| 精品久久久无码人妻中文字幕|