views:

101

answers:

2

select() is a great system call. You can pack any number of file descriptors, socket descriptors, pipes, etc. and get notified in a synchronous fashion when input becomes available.

Is there a way to create an interval/oneshot timer and use it with select()? That would save me from having multiple threads for IO and timing.

+3  A: 

timerfd_create does exactly this. It's a fairly recent addition to the linux kernel and might not be available on all distros yet though.

nos
Wow.. How did I not know about this? I must brush up on my linux syscalls.
Andrew Klofas
THere's a signalfd call too, in case you want signals delivered as fd events as well :-)
nos
+1  A: 

Use the timeout parameter - keep your timer events in a priority queue, check the top item and set the timeout accordingly - if the timeout is reached, then you can check that the event is ready to run, run the event and continue.

At least that's what I do.

Note that poll has a nicer interface (in some ways) and may be more efficient with lots of file descriptors.

MarkR