0

I need to use some timers in my code. I've got something like this:

    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);

Where c_thread is some simple function which is setting new timer, SECOND is:

#define SECOND 1000000000

c is something like 2.25

And my problem is that this timer doesn't call c_thread when it should. When i change CLOCK_PROCESS_CPUTIME_ID to CLOCK_REALTIME everything is ok, and it is called, but when I am using first one nothing happens. I am also checking CLOCK_PROCESS_CPUTIME_ID using other CLOCK_REALTIME timer with clock_gettime function and values of clock reach my it_value.

Any ideas what could be wrong?

And my second question: Is there any way to pass some arguments to function called as thread using timers?

  • Please give a [MCVE]. `CPUTIME` indicates how much cpu time the process consumes (stating the obvious). If your program goes to sleep indefinetely then it will never reach 2.5 seconds of cpu time. So we need to see what else the program does after setting the timer. – kaylum Dec 25 '16 at 01:21

1 Answers1

2

@annamataris problem was not related to spinlock and nanosleep stuff. There is reason to use only CLOCK_REALTIME because.

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.

Read man timer_create for more info.

Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19