Multithreading

Conventional GUI applications have one thread of execution and perform one operation at a time. If the user invokes a time-consuming operation from the user interface in a single-threaded application, the interface typically freezes while the operation is in progress. Chapter 7 (Event Processing) provides some solutions to this problem. Multithreading is another solution.

In a multithreaded Qt application, the GUI runs in its own thread and the processing takes place in one or more other threads. This results in applications that have responsive GUIs even during intensive processing. Another benefit of multithreading is that on multiprocessor machines different threads may be executed simultaneously on different processors, resulting in better performance.

In this chapter, we will start by showing how to subclass QThread and how to use QMutex, QSemaphore, and QWaitCondition to synchronize threads. Then we will see how to communicate with the GUI thread from non-GUI threads while the event loop is running, and round off with a review of which Qt classes can be used in non-GUI threads and which cannot.

Multithreading is a large topic with many books devoted exclusively to the subject. Here, it is assumed that you already understand the fundamentals of multithreaded programming; the focus is on explaining how to develop multithreaded Qt applications rather than on the subject of threading itself.

Категории