• <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 多彩人生 閱讀(2259) 評論(1)  編輯 收藏 引用

            評論

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

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

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产美女久久久| 99久久久精品免费观看国产| 欧美综合天天夜夜久久| 色综合合久久天天综合绕视看| 久久久精品午夜免费不卡| 丁香五月综合久久激情| 综合久久给合久久狠狠狠97色| 亚洲熟妇无码另类久久久| 久久综合综合久久综合| 热久久这里只有精品| 婷婷久久综合九色综合绿巨人| 亚洲精品无码久久久久| 狠狠综合久久综合中文88| 99久久国产精品免费一区二区 | 超级碰久久免费公开视频| 人妻丰满?V无码久久不卡| 精品九九久久国内精品| 久久影院亚洲一区| 国产91久久精品一区二区| 人妻无码αv中文字幕久久琪琪布| 久久er99热精品一区二区| 午夜精品久久久久久影视riav| 久久777国产线看观看精品| 国内精品伊人久久久久777| 久久久久99精品成人片| 久久精品国产久精国产| 亚洲精品高清国产一线久久| 亚洲国产精品一区二区三区久久 | 久久亚洲AV无码精品色午夜 | 国内精品久久久人妻中文字幕| 久久精品国产一区二区三区| AV无码久久久久不卡网站下载| 久久无码AV中文出轨人妻| 久久影视综合亚洲| 欧美性猛交xxxx免费看久久久| 品成人欧美大片久久国产欧美...| 久久夜色精品国产噜噜噜亚洲AV| 国产成人精品三上悠亚久久| 久久中文字幕无码专区| 久久免费99精品国产自在现线| 久久国产精品视频|