2011-05-17 76 views
1

我正在使用C++進行項目工作。如何使用Boost庫創建TimerHandler

我想被稱爲指定時間後TimerHandler,但在同一時間,我不希望阻止在下面的代碼當前線程或io.run之後的任何代碼():

#include <iostream> 
#include <string> 
#include <boost/format.hpp> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

class TimerTest 
{ 
public: 
    static void PrintOutTimerHandler(const boost::system::error_code&, const std::string& message) 
    { 
     std::cout << "PrintOutTimerHandler called: " << ", message: " << message << std::endl; 
    } 

    void run() 
    { 
     boost::asio::io_service io; 
     boost::asio::deadline_timer dt(io, boost::posix_time::seconds(5)); 

     std::cout << "Start:\t" << std::endl; 

     dt.async_wait(boost::bind(PrintOutTimerHandler, boost::asio::placeholders::error, std::string("here is the message"))); 

     // Do some job here 
     for (int i = 0; i < 1000000; ++i) 
      ++i, --i; 

     std::cout << "End:\t" << std::endl; 

     io.run(); 

     std::cout << "When to reach here 1: " << std::endl; 
    } 
}; 

int main() 
{ 
    TimerTest tt; 
    tt.run(); 

    std::cout << "When to reach here 2: " << std::endl; 

    return 0; 
} 

/* Current output: 
Start: 
End: 
PrintOutTimerHandler called: , message: here is the message 
When to reach here 1: 
When to reach here 2: 
*/ 

/* Expected output: 
Start: 
End: 
When to reach here 1: 
When to reach here 2: 
PrintOutTimerHandler called: , message: here is the message 
*/ 

我想我已經說清楚了。我的問題是:

  • 如果可以不 引入一個新的線程,諸如Flex 的ActionScript來解決,這是最好的,但 我想不會(我猜的ActionScript是 使用隱藏線);
  • 如果我們必須 引入一個額外的線程做 工作,你會介意爲我寫下 僞代碼嗎?

謝謝。

Peter

回答

2

這裏是一個例子。在一個單獨的線程

asio::io_service io_service; 
asio::thread t(boost::bind(&asio::io_service::run, &io_service)); 

運行io_service對象或線程組

boost::thread_group threads; 
for (std::size_t i = 0; i < my_thread_count; ++i) 
    threads.create_thread(boost::bind(&asio::io_service::run, &io_service)); 

在運行它記住,你的主線程應始終運行,因爲當它的存在也催生了將退出所有線程。

我希望這會有所幫助。

+0

感謝您的回覆。我試圖將新線程應用於我的示例。但我不知道如何,請你幫我一下? – 2011-05-17 15:49:40

+0

@OcrunC,請忽略我上面的評論。我得到了它的工作。謝謝。 – 2011-05-17 21:31:15

+0

你會介意看看我的另一篇文章:http://stackoverflow.com/questions/6076524/why-my-eventtimer-implemented-by-boostasioio-service-and-boostdeadline-time – 2011-05-20 19:33:33

1

我誤解了OrcunC說的,但實際上他是正確的。以下是修改後的版本供您參考:

#include <iostream> 
#include <string> 
#include <boost/format.hpp> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/thread.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

class TimerTest 
{ 
public: 
    static void PrintOutTimerHandler(const boost::system::error_code&, const std::string& message) 
    { 
     std::cout << "PrintOutTimerHandler called: " << ", message: " << message << std::endl; 
    } 

    TimerTest(unsigned int timeout) 
     : dt(io, boost::posix_time::milliseconds(timeout)) 
    { 
    } 

    void run() 
    { 
     std::cout << "Start:\t" << std::endl; 

     dt.async_wait(boost::bind(PrintOutTimerHandler, boost::asio::placeholders::error, std::string("here is the message"))); 

     boost::thread thrd(boost::bind(&boost::asio::io_service::run, &io)); 

     // Do some job here 
     for (int i = 0; i < 1000000; ++i) 
      ++i, --i; 

     std::cout << "End:\t" << std::endl; 

     std::cout << "When to reach here 1: " << std::endl; 
    } 

    boost::asio::io_service  io; 
    boost::asio::deadline_timer dt; 
}; 

int main() 
{ 
    TimerTest tt(5000); 
    tt.run(); 

    std::cout << "When to reach here 2: " << std::endl; 

    // Keep the main thread active for testing purpose. Otherwise, 
    // once the TimerTest object is destroyed when exiting the main() function, 
    // the sub thread spawed in tt.run() will also exit; 
    Sleep(10000); 
} 

/* Current output and Expected output: 
Start: 
End: 
When to reach here 1: 
When to reach here 2: 
PrintOutTimerHandler called: , message: here is the message 
*/ 
+0

只是一個補充 - CMakeLists .TXT需要建立上面的例子是: cmake_minimum_required(VERSION 2.8) 項目(OneShotTimer) find_package(升壓1.40.0 REQUIRED系統線程) INCLUDE_DIRECTORIES($ {INCLUDE_DIRECTORIES} $ {Boost_INCLUDE_DIRS}) add_executable( OneShotTimer OneShotTimer.cpp) TARGET_LINK_LIBRARIES(OneShotTimer $ {Boost_LIBRARIES}) – 2013-12-18 16:23:53

+0

處理程序永遠不會被我調用?我不得不改變睡眠(10000)到usleep(10000)來讓它編譯(我在Linux上),但我的輸出是「開始:\t 結束:\t 何時到達這裏1: 何時到達此處2:「 - 我從來沒有看到」PrintOutTimerHandler叫:消息:這裏是消息「就像你在輸出中顯示。 – 2013-12-18 16:35:43

相關問題