Linux 计时器 CLOCK_PROCESS_CPU_ID 不工作

Linux timer CLOCK_PROCESS_CPU_ID is not working

我需要在我的代码中使用一些计时器。我有这样的东西:

    struct sigevent ec;

    ec.sigev_notify = SIGEV_THREAD;
    ec.sigev_value.sival_ptr = &c_timer;
    ec.sigev_notify_function = c_thread;
    ec.sigev_notify_attributes = NULL;

    secs = floor(c);
    nsecs = (long long) SECOND * (c - secs);
    printf("%ds\t%lldns\n\n",secs,nsecs);
    it1c.it_value.tv_sec = secs;
    it1c.it_value.tv_nsec = nsecs;
    it1c.it_interval.tv_sec = 0;
    it1c.it_interval.tv_nsec = 0;

    timer_create(CLOCK_PROCESS_CPUTIME_ID, &ec, &c_timer);
    timer_settime(c_timer, 0, &it1c, NULL);

其中 c_thread 是一些设置新定时器的简单函数,SECOND 是:

#define SECOND 1000000000

c 类似于 2.25

我的问题是这个计时器没有在它应该调用的时候调用 c_thread。当我将 CLOCK_PROCESS_CPUTIME_ID 更改为 CLOCK_REALTIME 时,一切正常,并且它被调用,但是当我使用第一个时,什么也没有发生。我也在检查 CLOCK_PROCESS_CPUTIME_ID 使用其他具有 clock_gettime 功能的 CLOCK_REALTIME 计时器,时钟值达到我的 it_value.

有什么想法是错误的吗?

我的第二个问题:有没有什么方法可以使用计时器将一些参数传递给称为线程的函数?

@annamataris 问题与自旋锁和 nanosleep 无关。有理由只使用 CLOCK_REALTIME 因为。

The POSIX timers system calls first appeared in Linux 2.6. Prior to this, glibc provided an incomplete user-space implementation (CLOCK_REALTIME timers only) using POSIX threads, and in glibc versions before 2.17, the implementation falls back to this technique on systems running pre-2.6 Linux kernels.

阅读 man timer_create 了解更多信息。