This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Using sem_timedwait

Hello Everyone,

I'm trying to use sem_timedwait in a user application - I've inlcuded semaphore.h [which defines __need_timespec, and includes time,h]. I'm also pointing to the correct include files that are part of the SDK release. I've based my code on this example - http://linux.die.net/man/3/sem_timedwait

However, when I cross compile the code, it tells me that CLOCK_REALTIME as undefined, and timespec tv_sec cannot be resolved. Any clue as to what I need to include in order to get the code working?

Thanks a lot in advance.

Regards

Santhosh

  • Have you tried to boot with RT kernel (real time) and execute there ?
  • Hello Titus,

    Sorry for the long delay in replying back to you. Here are some updates:

    1. I've added -lrt to my linker and now the code compiles.

    2. When I run the application I don't see the correct behavior when I used CLOCK_MONOTONIC_RAW. However, if I use CLOCK_REALTIME I get the correct results. 

    I wrote a sample program to wait for a semaphore for a maximum of 10 sec - when I use CLOCK_MONOTONIC_RAW, there is no wait at all. But CLOCK_REALTIME gives me correct max 10 sec delay. 

    Does this have to do with some kernel configuration option? 

    Thank you for your reply.

    Regards

    Santhosh

  • Hi Santhosh,
    It seems, iIt does not requires any kernel configuration.
    The "lrt" is required to avoid all "undefined" issues. or "-Wl,--no-as-needed" flag.

    Better to use "CLOCK_REALTIME"
  • Hello Titus,

    Thank you for your reply. I don't think using CLOCK_REALTIME is a good option for my application - primarily because if the RTC time is updated during the wait period, then the wait period will not be accurate. If the clock is set to time that is in the future or past, the REALTIME variable will be incorrect. I think CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW would be better option.

    What are your thoughts?

    Thank you
    Regards

    Santhosh
  • Hello Titus,

    I found some more details and yes you are correct. I need to use CLOCK_REALTIME as sem_timedwait uses time since epoch (1970...). And I need to take care that if system time changes, I should reset the thread using this timed semaphore.

    Thanks once again for your reply.
    Regards
    Santhosh