使用clock()函数调度任务时发生问题 [英] Issue when scheduling tasks using clock() function

查看:117
本文介绍了使用clock()函数调度任务时发生问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以不同的时间间隔调度任务:0.1秒,0.9秒.... 2s等
我使用clock()C ++函数,返回自模拟开始以来的tick数并且我使用CLOCKS_PER_SEC将ticks数转换为秒,但我注意到,当时刻是一个float,但是当它是一个整数时,任务不被调度。这里代码的部分负责调度:

I would like to schedule tasks at different time intervals: at 0.1 sec, 0.9s .... 2s etc I use the clock() C++ function that returns the number of ticks since the beginning of the simulation and I convert the ticks number to seconds using CLOCKS_PER_SEC but I have noticed that the task isn't scheduled when the instant is a float, but when it's an integer it does. Here the portion of the code responsible for the scheduling:

float goal = (float) clock() / CLOCKS_PER_SEC + 0.4 ;  // initially (float) clock() / CLOCKS_PER_SEC = 0 ; 
if ((float) clock() / CLOCKS_PER_SEC == goal) 
     do stuff ; 

在这种情况下,它不工作,但是当我安排任务在3秒,例如它工作。是精度的问题吗?

In that case, it doesn't work, but when I schedule the task to be done in 3 seconds for instance it works. Is it a problem of precision??

推荐答案

如果我在C ++中实现一些计时器机制,我可能会使用 std :: chrono 命名空间以及 std :: priority_queue

If I was to implement some timer mechanism in C++, I would probably be using the std::chrono namespace together with std::priority_queue.

#include <functional>
#include <queue>
#include <chrono>
#include <sys/time.h>  // for `time_t` and `struct timeval`

namespace events
{
    struct event
    {
        typedef std::function<void()> callback_type;
        typedef std::chrono::time_point<std::chrono::system_clock> time_type;

        event(const callback_type &cb, const time_type &when)
            : callback_(cb), when_(when)
            { }

        void operator()() const
            { callback_(); }

        callback_type callback_;
        time_type     when_;
    };

    struct event_less : public std::less<event>
    {
            bool operator()(const event &e1, const event &e2) const
                {
                    return (e2.when_ < e1.when_);
                }
    };

    std::priority_queue<event, std::vector<event>, event_less> event_queue;

    void add(const event::callback_type &cb, const time_t &when)
    {
        auto real_when = std::chrono::system_clock::from_time_t(when);

        event_queue.emplace(cb, real_when);
    }

    void add(const event::callback_type &cb, const timeval &when)
    {
        auto real_when = std::chrono::system_clock::from_time_t(when.tv_sec) +
                         std::chrono::microseconds(when.tv_usec);

        event_queue.emplace(cb, real_when);
    }

    void add(const event::callback_type &cb,
             const std::chrono::time_point<std::chrono::system_clock> &when)
    {
        event_queue.emplace(cb, when);
    }

    void timer()
    {
        event::time_type now = std::chrono::system_clock::now();

        while (!event_queue.empty() &&
               (event_queue.top().when_ < now))
        {
            event_queue.top()();
            event_queue.pop();
        }
    }
}

使用 events :: add ,每秒调用 events :: timer 几次。

示例:

void foo()
{
    std::cout << "hello from foo\n";
}

void done()
{
    std::cout << "Done!\n";
}

struct bar
{
    void hello()
        { std::cout << "Hello from bar::hello\n"; }
};

auto now = std::chrono::system_clock::now();
bar b;

events::add(foo, now + std::chrono::seconds(2));

events::add(std::bind(&bar::hello, b), now + std::chrono::seconds(4));

events::add(done, now + std::chrono::seconds(6));

while (true)
{
    usleep(10000);
    events::timer();
}

上述示例将打印:


hello from foo
hello from bar::hello
Done!

每两秒打印一行。 完成!后,程序将永远循环,不做任何操作。

One line will be printed every two second. After "Done!" the program will just loop forever, doing nothing.

C ++ 11功能,但已经用GCC 4.4.5和4.7.1测试。 VC ++ 2010不幸没有< chrono> 头,但VC ++ 2012RC显然有它。

Note that this program contains lots of C++11 functionality, but has been tested with GCC 4.4.5 and 4.7.1. VC++2010 unfortunately does not have the <chrono> header, but the VC++2012RC apparently have it.

这篇关于使用clock()函数调度任务时发生问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆