使用clock()函数调度任务时出现问题

2023-12-04

我想以不同的时间间隔安排任务:0.1 秒、0.9 秒 .... 2 秒等 我使用 Clock() C++ 函数返回自模拟开始以来的滴答数,并使用 CLOCKS_PER_SEC 将滴答数转换为秒,但我注意到当瞬时为浮点时,任务不会被调度,但是当它是一个整数。这是负责调度的代码部分:

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 秒内完成时,它就起作用了。是精度问题吗??


如果我要在 C++ 中实现一些计时器机制,我可能会使用std::chrono名称空间和...一起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每秒几次。

Simple example:

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);  // TODO: Do some "real" work here instead...
    events::timer();
}

上面的例子将打印:



hello from foo
hello from bar::hello
Done!
  

每两秒打印一行。后"Done!"该程序将永远循环,什么都不做。

请注意,该程序包含大量 C++11 功能,但已使用 GCC 4.4.5 和 4.7.1 进行了测试。不幸的是VC++2010没有<chrono>标头,但 VC++2012RC 显然有它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用clock()函数调度任务时出现问题 的相关文章

随机推荐