簡體   English   中英

如何使用Boost庫創建TimerHandler

[英]How To Create TimerHandler Using Boost Library

我正在使用C ++進行項目。

我希望在指定時間后調用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使用的是隱藏線程)。
  • 如果我們必須引入額外的線程來完成這項工作,您介意為我寫下偽代碼嗎?

謝謝。

彼得

這是一個例子。 在單獨的線程中運行io_service

asio::io_service io_service;
asio::thread t(boost::bind(&asio::io_service::run, &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));

請記住,主線程應始終運行,因為當它存在時,所有產生的線程也將退出。

我希望這有幫助。

我誤解了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
 */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM