C++螺纹秒表

发布于 2024-10-01 04:49:30 字数 1148 浏览 3 评论 0原文

我正在尝试用 C++ 创建一个秒表,类似于 Java 的 定时器任务。我喜欢他们的库,因为它已经内置了线程。我已经看到 Boost Chrono,但是,它仍在开发中,并且不想使用它。

我当前的实现并不走运(我现在正在记忆中,所以这可能是一些伪代码)。

boost::asio::io_service io;
boost::asio::deadline_timer timer(io);


Initialize()
{
  boost::shared_ptr<boost::thread> thread = boost::shared_ptr<boost::thread>(
      new boost::thread(
        boost::bind(&boost::asio::io_service::run, &io)
  );
}

Start()
{
   timer.async_wait(
       bind(&HandleTimerEvent, this, asio::placeholders::error)
   );
}

Stop()
{
  timer.cancel
}

Tick()
{
   cout << "THE TIME IS: " << timer.current_time << endl; // Pseudo code for current_time.
}

HandleTimerEvent(boost::system::error_code& e)
{
  cout << "Timer has ended: " << e << endl;
}

我想要的是让线程连续调用 Tick() 并打印出当前时间。另外,我当前的实现似乎有线程阻塞应用程序的其余部分,这是我绝对不想要的。我该如何做这两件事?

我在线程方面是个菜鸟,所以如果我说了什么似乎不正确的事情,请原谅我。

I am trying to create a stopwatch in C++, something similar to Java's TimerTask. I like their library since it already has threading built in. I have seen Boost Chrono, however, it's still in development and would prefer not to use it.

My current implementation has been without luck (I am going by memory right now so this might be a little pseudo code like).

boost::asio::io_service io;
boost::asio::deadline_timer timer(io);


Initialize()
{
  boost::shared_ptr<boost::thread> thread = boost::shared_ptr<boost::thread>(
      new boost::thread(
        boost::bind(&boost::asio::io_service::run, &io)
  );
}

Start()
{
   timer.async_wait(
       bind(&HandleTimerEvent, this, asio::placeholders::error)
   );
}

Stop()
{
  timer.cancel
}

Tick()
{
   cout << "THE TIME IS: " << timer.current_time << endl; // Pseudo code for current_time.
}

HandleTimerEvent(boost::system::error_code& e)
{
  cout << "Timer has ended: " << e << endl;
}

What I would like to have is to have the thread continuously call Tick() and print out the current time. Also, my current implementation seems to have the thread as blocking the rest of the application which is something I definitely don't want. How would I do both of these things?

I am a noob when it comes to threading so please forgive me if I stated something that doesn't seem right.

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

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

发布评论

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

评论(3

对你再特殊 2024-10-08 04:49:30

您几乎已经完成了,因为您正在创建一个新线程来处理 io_service::run() ,您的主线程将不会阻塞。您需要做两件事,

  1. 确保在 Initialize() 之前调用 Start() (使用相同的 io_service 实例),这样 io_service 就可以做一些事情,否则就会退出!
  2. 在您的 HandleTimer() 方法中,再次调用 async_wait 来排队下一个时钟周期,否则 io_service 将退出,因为它无事可做。

You nearly have it, because you're creating a new thread to handle io_service::run(), your main thread will not block. Two things you need to do,

  1. ensure you call Start() before you Initialize() (using the same io_service instance), this way there is something for the io_service to do, else it will quit!
  2. On your HandleTimer() method, call async_wait again to queue up the next tick, else io_service will quit as it has nothing to do..
北城孤痞 2024-10-08 04:49:30

您读过 Boost.Asio 异步 截止日期计时器教程吗?添加多个线程 通过创建线程池来调用io_service::run

多线程可以调用
io_service::run() 设置一个池
完成处理程序所在的线程
可以被调用。这种做法还可以
与 io_service::post() 一起使用
执行任何计算的手段
跨线程池的任务。

请注意,所有已加入的线程
考虑 io_service 的池
等价的,并且 io_service 可以
在他们之间分配工作
任意时尚。

Have you read the Boost.Asio asynchronous deadline timer tutorial? It's fairly trivial to add multiple threads by creating a thread pool to invoke io_service::run.

Multiple threads may call
io_service::run() to set up a pool of
threads from which completion handlers
may be invoked. This approach may also
be used with io_service::post() to use
a means to perform any computational
tasks across a thread pool.

Note that all threads that have joined
an io_service's pool are considered
equivalent, and the io_service may
distribute work across them in an
arbitrary fashion.

一刻暧昧 2024-10-08 04:49:30

这对我有用:

#include <sys/time.h>

static struct timeval tv_s, tv_e;

static void timer_start()
{
   gettimeofday( &tv_s, NULL );
}

static unsigned long timer_stop()
{
   gettimeofday( &tv_e, NULL );
   return (tv_e.tv_sec - tv_s.tv_sec ) * 1000000ul
      + ( tv_e.tv_usec - tv_s.tv_usec );
}

This works for me:

#include <sys/time.h>

static struct timeval tv_s, tv_e;

static void timer_start()
{
   gettimeofday( &tv_s, NULL );
}

static unsigned long timer_stop()
{
   gettimeofday( &tv_e, NULL );
   return (tv_e.tv_sec - tv_s.tv_sec ) * 1000000ul
      + ( tv_e.tv_usec - tv_s.tv_usec );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文