0

Where/how I can get a Looper according to the C++ standard or C++ standard libraries ?

I need to design my own callback system and, of course, I need this one to manage my queue and my components.

A looper is something that given a frequency does 1 simple thing, it just runs a queue at each clock, if you set a 10 ms looper, every 10ms the looper will fire the event/events in the queue.

This is basically a looper, most of the time is usually tied to the kernel, the hardware clock or something really low-level.

Mat
  • 202,337
  • 40
  • 393
  • 406
axis
  • 874
  • 2
  • 7
  • 13
  • 8
    What is a "Looper"? Is this from a book or something? Need more context please... – tenfour Oct 06 '12 at 12:43
  • @tenfour I guess the OP means something like a for_each operation on a STL container. Question at the OP, why not just using one of the STL functional methods? – πάντα ῥεῖ Oct 06 '12 at 12:46
  • @kotlinski Good guess, maybe more right than mine. That shows what unclear questions can lead to ... – πάντα ῥεῖ Oct 06 '12 at 12:48
  • @g-makulik it's not unclear, the word looper means exactly 1 thing. – axis Oct 06 '12 at 12:49
  • From the updated description it looks like OP wants to create a timer and handle timer callback. – Naveen Oct 06 '12 at 12:51
  • @Naveen i want a looper ... http://www.youtube.com/watch?v=TkPiXRNee7A#t=27m50s I don't know how to express this in other kind of communications ... – axis Oct 06 '12 at 12:52
  • sounds like a sort of a cronjob to me? – Rob Oct 06 '12 at 12:53
  • @axis OK, then the term is just new to me. I usually call such thing an event queue triggered thread loop. By means of C++ standards this needs to involve threads and mutexes/semaphores which is available with C++11. – πάντα ῥεῖ Oct 06 '12 at 12:53
  • Don't confuse C++ with magic. You can't just have something happening to your program outside its own control flow. You can either design your entire application around an multiplexing event loop and use something like a timerfd in Linux, or you have to design your own multithreaded components - but then it's highly non-trivial to synchronise data access. – Kerrek SB Oct 06 '12 at 12:59
  • @KerrekSB even a toaster has its own timer ... i need to go multi-threaded for just having a queue + a looper ? Also i don't want to break any flow, i just need ... a looper, something that can keep firing events at a precise rate . – axis Oct 06 '12 at 13:02
  • 1
    @axis: Think about it again. That's just not how C++ works. A program has a precise notion of control flow. Stuff doesn't just happen outside of that. A toaster is basically just a big `sleep` statement, but notice how the toaster *isn't doing anything else* while toasting. – Kerrek SB Oct 06 '12 at 13:03
  • @axis: Well, basically the toaster will use `heat(); sleep(time); cooldown();`. – Zeta Oct 06 '12 at 13:04
  • If I understand well, a looper is just a main loop. Well, is a while loop calling `boost::signals` objects enough ? Glib has a GMainLoop object if you want to go with plain C. – Alexandre C. Oct 06 '12 at 13:05
  • 6
    @Axis: For you it might have a single meaning, but it is not a standard or common one. To be honest the only time I have heard that word before was in slang referring to some particular type of *happy pill*. Never in a technical context. The guys that did that video might want to look cool – David Rodríguez - dribeas Oct 06 '12 at 13:07
  • @AlexandreC. nope because a Looper has 2 big differences: A) doesn't care about functions, it just fires up the queue B) it can be set to a particular frequency – axis Oct 06 '12 at 13:07
  • @KerrekSB how i can have my callback mechanism in C++ at this point ? – axis Oct 06 '12 at 13:08
  • @axis: A) is handled by `boost::signals`, B) is solved by putting a timer in the main loop. You just need a thread safe queue, that you can implement yourself using semaphores and `std::deque`, or that you can get somewhere else (look up intel TBB) – Alexandre C. Oct 06 '12 at 13:21
  • 3
    @axis: first hit on google [Looper](http://www.imdb.fr/title/tt1276104/). Clear maybe, but not helping much :p – Matthieu M. Oct 06 '12 at 13:25

2 Answers2

2

You can use some primitives from the new C++11 standards to create such thing. Use std::thread and std::timed_mutex to control a loop reading from your std::queue. Put functor classes in your queue. Execute functors from within the thread loop.

You can have it also vice versa pushing functor objects 'upwards' the queue at a constant frequency and execute them from a 'client' side.

Somehow it boils down to have a time controlled access to the queue from the 'Looper' thread. You can use e.g. std::thread::sleep_for() to do this, or use some more sophisticated mechanism that behaves like a timed semaphore (e.g. a condition variable coupled with a std::timed_mutex).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

QTimer does what you want -- "single-shot", or as in your case, fires repeatedly at a given (millisecond) frequency.

You might also google for "watch-dog-timer", as I think that may be a more common term than "looper".

If you want to go more lower-level, some systems (like Windows) have a "system-clock" (typically at millisecond resolution), and another higher-performance clock like a "multi-media-clock" (typically at nanosecond resolution) if performance is important.

[EDIT], Ok, so I watched the video on "what-is-a-looper". This looks like a standard event-processing-queue. GUI events trigger messages-added-to-the-queue, and the "looper" clears/executes-the-message-queue periodically. A special case is that the "looper" also handles "local-service-calls" (on Android, which was the topic of the video). So, it seems you merely need:

  • a message queue
  • a timer to clear the message queue (on a thread, or separate from the "main-processing")

The video notes:

  • Views use Looper messages to fire events
  • Since Loopers are 1:1 with threads, the View tree is too
  • Threads you create cannot directly touch a View
  • But, you can create a new Looper for your own thread

So, interesting, but doesn't look particulary difficult to implement. This is a well-tread pattern.

So, in this context, my suggestion of QTimer would only be a part of the solution. Sounds like you want the library for the message queue to go with it.

charley
  • 5,913
  • 1
  • 33
  • 58
  • That doesn't match much the OP's requirement for "according to the C++ standard or C++ standard libraries" – πάντα ῥεῖ Oct 06 '12 at 12:56
  • "watch-dog-timer" ... what kind of word is that ? By the way you seems to understand my problem but your solution is not really standard, i can't believe that C++ does not support callbacks from the scratch or similar mechanism ... – axis Oct 06 '12 at 12:59
  • `watch-dog-timer` has history with hardware. A timer "pings" periodically to get a response, and if no response is had, it triggers action (like a hardware board reset). Goal is "ultimate-independence" from the "main-system-processing". It is used in software for independent operations, like "clearing-message-queues". – charley Oct 06 '12 at 13:08
  • 2
    @axis, C++ supports callbacks and mechanisms like this through *libraries*. You want to write, or use, a library. C++11 added standards for `thread` libraries. However, "callbacks" and "message-queues" can be highly app-specific, and we've had these for decades (the basis for most all GUI libraries), and they are designed to handle specific constraints (e.g., single-or-many-threads, high-responsiveness, etc.) Further, what you want is also handled by merely using Inter-Process-Communication (IPC), which does the same thing (many processes merely forward their messages to a service process) – charley Oct 06 '12 at 13:13