user965972
user965972

Reputation: 2597

Equivalent of NSRunLoop under linux (Raspberry Pi)?

OS X has the NSRunLoop which just sits around waiting for timers and sources to fire. Then Apple switch to Grand Central Dispatch (GCD) where you have dispatch_main() to keep the app alive and a bunch of dispatch_source_'s to schedule stuff or to get notifications (from sockets or user actions). If nothing is happening, the app is just idle and doesn't use any CPU power.

Now I want to learn how to write a driver with c++. So I have a raspberry pi (so linux) and once in a while data is coming in from a socket or interrupt.

Instead of polling I want to work with events. So I am searching for the equivalent of NSRunLoop for c++ and linux.

Though I would also like to learn how something like that is or can be implemented. In pseudocode the run loop acts something like this, as far as I know,

timeout = 0
while (true) {
   wait(timeout) || wait for source event other than timer

   loop all timers
      if timer fired
         run timer action

   loop all timers
       timeout = min(timeout, timer.timeNextEvent)

   loop all sources
        if source hasData
           run source action      
}

The thing I don't get is the wait function at the top. How do you wait for a source lets say a timer to fire without going into sleep mode?

I have found many examples of polling and timers that go into sleep mode. But I want to avoid sleep and just wait on interrupts or signals or a user generated event like keyboard input on the command line.

Any pointers on how to proceed?

Upvotes: 0

Views: 191

Answers (1)

Rishabh Agrawal
Rishabh Agrawal

Reputation: 122

Probably it's late for this answer but in case someone ends up here, epoll should be the answer. https://man7.org/linux/man-pages/man7/epoll.7.html

Upvotes: 0

Related Questions