如何使用 Boost 库创建 TimerHandler

发布于 2024-11-07 12:10:18 字数 1779 浏览 0 评论 0原文

我正在开发一个使用 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 是 使用隐藏线程);
  • 如果我们必须 引入一个额外的线程来执行 工作,你介意写下 我的伪代码?

谢谢。

彼得

I'm working on a project using C++.

I want a TimerHandler to be called after a specified time, but at the same time I don't want to block the current thread or any code after io.run() in the following code:

#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
 */

I think I made myself clear. My questions are:

  • If this can be solved without
    introducing a new thread, like Flex
    ActionScript, that's is the best, but
    I guess not (I guess ActionScript is
    using a hidden thread);
  • If we have to
    introduce an extra thread to do the
    job, would you mind writing down the
    pseudo code for me?

Thanks.

Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-11-14 12:10:18

这是一个例子。在单独的线程中运行 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));

请记住,主线程应该始终运行,因为当它存在时,生成的所有线程也将退出。

我希望这有帮助。

Here is an example . Run the io_service in a separate thread

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

or run it in a thread group

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));

Remember that your main thread should always run because when it exists all threads spawned will also exit.

I hope this helps.

つ低調成傷 2024-11-14 12:10:18

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

I misunderstood what OrcunC said, but actually he is correct. Here is the modified version for your reference:

#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
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文