使用 time 和 difftime 创建超时

发布于 2024-11-03 20:08:47 字数 1722 浏览 0 评论 0原文

gcc (GCC) 4.6.0 20110419 (Red Hat 4.6.0-5)

我正在尝试获取开始时间和结束时间。并了解它们之间的差异。

我的功能是为我们现有的硬件创建 API。

API wait_events 采用一个参数,即以毫秒为单位的时间。所以我试图在 while 循环之前开始。并使用 time 来获取秒数。然后,经过 1 次循环迭代后,获取时间差,然后将该差值与超时进行比较。

非常感谢您的任何建议,

/* Wait for an event up to a specified time out.
 * If an event occurs before the time out return 0
 * If an event timeouts out before an event return -1 */
int wait_events(int timeout_ms)
{
    time_t start = 0;
    time_t end = 0;
    double time_diff = 0;
    /* convert to seconds */
    int timeout = timeout_ms / 100;

    /* Get the initial time */
    start = time(NULL);
    while(TRUE) {
        if(open_device_flag == TRUE) {
            device_evt.event_id = EVENT_DEV_OPEN;
            return TRUE;
        }
        /* Get the end time after each iteration */
        end = time(NULL);
        /* Get the difference between times */
        time_diff = difftime(start, end);
        if(time_diff > timeout) {
            /* timed out before getting an event */
            return FALSE;
        }
    }
}

将调用的函数将是这样的。

int main(void)
{
#define TIMEOUT 500 /* 1/2 sec */
    while(TRUE) {
        if(wait_events(TIMEOUT) != 0) {
            /* Process incoming event */
            printf("Event fired\n");
        }
        else {
            printf("Event timed out\n");
        }
    }

    return 0;
}

=============== 编辑更新结果==================

1) With no sleep -> 99.7% - 100% CPU
2) Setting usleep(10) -> 25% CPU
3) Setting usleep(100) -> 13% CPU
3) Setting usleep(1000) -> 2.6% CPU
4) Setting usleep(10000) -> 0.3 - 0.7% CPU
gcc (GCC) 4.6.0 20110419 (Red Hat 4.6.0-5)

I am trying to get the time of start and end time. And get the difference between them.

The function I have is for creating a API for our existing hardware.

The API wait_events take one argument that is time in milli-seconds. So what I am trying to get the start before the while loop. And using time to get the number of seconds. Then after 1 iteration of the loop get the time difference and then compare that difference with the time out.

Many thanks for any suggestions,

/* Wait for an event up to a specified time out.
 * If an event occurs before the time out return 0
 * If an event timeouts out before an event return -1 */
int wait_events(int timeout_ms)
{
    time_t start = 0;
    time_t end = 0;
    double time_diff = 0;
    /* convert to seconds */
    int timeout = timeout_ms / 100;

    /* Get the initial time */
    start = time(NULL);
    while(TRUE) {
        if(open_device_flag == TRUE) {
            device_evt.event_id = EVENT_DEV_OPEN;
            return TRUE;
        }
        /* Get the end time after each iteration */
        end = time(NULL);
        /* Get the difference between times */
        time_diff = difftime(start, end);
        if(time_diff > timeout) {
            /* timed out before getting an event */
            return FALSE;
        }
    }
}

The function that will call will be like this.

int main(void)
{
#define TIMEOUT 500 /* 1/2 sec */
    while(TRUE) {
        if(wait_events(TIMEOUT) != 0) {
            /* Process incoming event */
            printf("Event fired\n");
        }
        else {
            printf("Event timed out\n");
        }
    }

    return 0;
}

=============== EDIT with updated results ==================

1) With no sleep -> 99.7% - 100% CPU
2) Setting usleep(10) -> 25% CPU
3) Setting usleep(100) -> 13% CPU
3) Setting usleep(1000) -> 2.6% CPU
4) Setting usleep(10000) -> 0.3 - 0.7% CPU

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

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

发布评论

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

评论(2

北风几吹夏 2024-11-10 20:08:47

你把它变得过于复杂了 - 简化了:

time_t start = time();
for (;;) {
    // try something
    if (time() > start + 5) {
        printf("5s timeout!\n");
        break;
    }
}

time_t 通常应该只是一个 intlong int ,具体取决于您的平台计算自此以来的秒数1970 年 1 月 1 日。

旁注:

int timeout = timeout_ms / 1000;

一秒包含 1000 毫秒。

编辑-另一个注释:
您很可能必须确保其他线程和/或事件处理可以发生,因此包括某种线程不活动状态(使用 sleep()nanosleep()< /code> 或其他)。

You're overcomplicating it - simplified:

time_t start = time();
for (;;) {
    // try something
    if (time() > start + 5) {
        printf("5s timeout!\n");
        break;
    }
}

time_t should in general just be an int or long int depending on your platform counting the number of seconds since January 1st 1970.

Side note:

int timeout = timeout_ms / 1000;

One second consists of 1000 milliseconds.

Edit - another note:
You'll most likely have to ensure that the other thread(s) and/or event handling can happen, so include some kind of thread inactivity (using sleep(), nanosleep() or whatever).

我为君王 2024-11-10 20:08:47

如果不调用 Sleep() 函数,这是一个非常糟糕的设计:您的循环将使用 100% 的 CPU。即使您正在使用线程,其他线程也不会有太多时间运行,因为该线程将使用许多 CPU 周期。
您应该设计类似的东西:

while(true) {
  Sleep(100); // lets say you want a precision of 100 ms
  // Do the compare time stuff here
}

如果您需要计时精度并使用不同的线程/进程,请使用互斥体(信号量,增量/减量为 1) 或关键部分,以确保函数的时间比较不会被您自己的另一个进程/线程中断。
我相信您的 Red Hat 是 System V,因此您可以使用 IPC

Without calling a Sleep() function this a really bad design : your loop will use 100% of the CPU. Even if you are using threads, your other threads won't have much time to run as this thread will use many CPU cycles.
You should design something like that:

while(true) {
  Sleep(100); // lets say you want a precision of 100 ms
  // Do the compare time stuff here
}

If you need precision of the timing and are using different threads/processes, use Mutexes (semaphores with a increment/decrement of 1) or Critical Sections to make sure the time compare of your function is not interrupted by another process/thread of your own.
I believe your Red Hat is a System V so you can sync using IPC

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