Reputation: 22916
http://doc.qt.io/qt-5/threads-qobject.html#per-thread-event-loop
https://wiki.qt.io/Threads_Events_QObjects#Per-thread_event_loop
These two links talk about event loop. Please explain the "meaning" of the term "event loop" w.r.t threads in Qt?
Upvotes: 0
Views: 603
Reputation: 8355
An event loop is usually a loop that is run by the main thread to receive events that either originate from the system (e.g. GUI interaction, network events, timers, ...) or from other Qt components (e.g. QCoreApplication::postEvent()
, ...). The event loop waits for new events to arrive in the event queue, then takes them out of the queue and sends them to their destination QObject
where they are handled by an overridden QObject::event(QEvent*)
(e.g. A QPushButton
would handle a mouse press event by emitting pressed()
, ...).
Per-thread event loops are a generalization of the concept above. This makes it possible to handle events in worker threads by introducing the concept of a QObject
's thread affinity. Thread affinity is the thread in which a particular QObject
should have its events handled (the thread from which QObject::event
gets called for this QObject
). In the big picture, this can be used to run asynchronous code in worker threads (since GUI code should be run only in the main thread). For example, you can run many asynchronous sockets and have QTimer
s to disconnect these sockets after some specified time of inactivity all in a single worker thread. Per-thread event loops are also essential for cross-thread signal-slot connections, since this kind of signal emissions is translated into QMetaCallEvent
s under the hood (to be delivered to its destination QObject
).
Upvotes: 1