Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The C++11 way to build an event loop

What's the basic structure of a event loop system in C++11? How are the key elements (such as message queue, message dispatcher, signal) implemented? For example, do I still need a std::queue<Message>, a std::mutex and a std::condition_variable as what I did in c++98 + boost way? Also, the performance matters in the solution I'm seeking for.

like image 995
GuLearn Avatar asked May 16 '13 19:05

GuLearn


People also ask

What is CPP event loop?

An event loop, or sometimes called a message loop, is a thread that waits for and dispatches incoming events. The thread blocks waiting for requests to arrive and then dispatches the event to an event handler function. A message queue is typically used by the loop to hold incoming messages.

What is event loop QT?

Qt's event loop starts the moment the underlying application's exec() function gets called. Once started, the loop repeatedly checks for something to happen in the system, such as user-input through keyboard/mouse.

What are different event loops?

MainAppLoop - This is the main loop of an application. Typically this is found at the bottom of the main. It's exit usually signals the desire for the application to close down. There can only be one of these per application. ThreadLoop - This is the loop commonly found at the bottom of a UI Thread's main procedure.


1 Answers

Do it roughly the same way you would have done it in C++98. You can replace some platform-specific things like pthread_t, pthread_mutex, and pthread_cond with standardized equivalents (std::thread, std::{recursive_,}{timed_,}mutex, and std::condition_variable{,_any}), but the basic design is the same.

As @beerboy mentioned, Boost.Asio might be a good place to start even though AFAIK it hasn't been updated for C++11 yet.

like image 161
Jeffrey Yasskin Avatar answered Oct 15 '22 08:10

Jeffrey Yasskin