0

I have been reading Signal Handler in a separate pthread using timer_create, and I decided to try implementing a C example for a periodic timer function based on the discussion there, and compiling it under MINGW64 for Windows.

It turns out, while MINGW64 can indeed emulate some aspects of POSIX under Windows, timer_create and sigevent are not part of that (/mingw64/include/signal.h does not contain declaration for sigevent - while MSYS2 /usr/include/signal.h, via /usr/include/sys/signal.h, does) - I get these errors:

test.c:153:1: error: unknown type name 'timer_t'; did you mean 'time_t'?
  553 | timer_t timerId = 0;
      | ^~~~~~~
      | time_t
test.c: In function 'init_timer':
test.c:160:10: error: variable 'sev' has initializer but incomplete type
  560 |   struct sigevent sev = { 0 };
      |          ^~~~~~~~
test.c:160:27: warning: excess elements in struct initializer
  560 |   struct sigevent sev = { 0 };
      |                           ^
test.c:160:27: note: (near initialization for 'sev')
test.c:160:19: error: storage size of 'sev' isn't known
  560 |   struct sigevent sev = { 0 };
      |                   ^~~
test.c:170:22: error: 'SIGEV_THREAD' undeclared (first use in this function)
  570 |   sev.sigev_notify = SIGEV_THREAD;
      |                      ^~~~~~~~~~~~
test.c:170:22: note: each undeclared identifier is reported only once for each function it appears in
test.c:174:9: warning: implicit declaration of function 'timer_create'; did you mean 'timerclear'? [-Wimplicit-function-declaration]
  574 |   res = timer_create(CLOCK_REALTIME, &sev, &timerId);
      |         ^~~~~~~~~~~~
      |         timerclear
test.c:180:9: warning: implicit declaration of function 'timer_settime'; did you mean 'timeGetTime'? [-Wimplicit-function-declaration]
  580 |   res = timer_settime(timerId, 0, &its, NULL);
      |         ^~~~~~~~~~~~~
      |         timeGetTime
test.c:160:19: warning: unused variable 'sev' [-Wunused-variable]
  560 |   struct sigevent sev = { 0 };

So, what I want to achieve, is a periodic timer callback of a function at a period of 10 ms (as tight as possible (*)), with C code that can compile with gcc under both MINGW64 and Linux.

Is it possible to achieve this - and if so, how? (maybe there is a library that facilitates this?)

(*) - in my experiments with MINGW64 builds on Windows 10, I've seen that nanosleep(&ts, &ts); in a while loop in main() with timestamp printouts, -- where ts = {.tv_sec = 0, .tv_nsec = 500 * 1000000} describes an interval of half a second, -- can measure anywhere between 505 and 520 ms; so I'm not sure how "tight" of a 10 ms period will be achievable - but I'd still love to know about options to implement a cross-platform periodic timer, even with a poorer timer resolution ...

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • One possibility seems to be, to use `preciseSleep` from https://blog.bearcats.nl/accurate-sleep-function/ which uses `std::chrono::high_resolution_clock` which is a C++11 feature; which then means figuring out how to write and compile the timer function in C++11, so it is callable/usable from C ... – sdbbs Nov 16 '22 at 02:02

0 Answers0