- Options
- Thread Basics
- Native Threads and a GUI
- Qt Threading
- An Example
- Conclusion
Thread Basics
Threads are singularly aptly named: A thread is a single strand of execution within the process of your application. That means that inside your application, more than one thing can happen at the same timevirtually, if you've only got one processor, and really if you've got a multiprocessor box. All threads can have direct access to the data in you application.
That doesn't mean that it's a good idea to access that data from several threads at the same time, though. Consider, for instance, what would happen if Thread 1 places the cursor at the position 10,10 on your widget because it wants to paint a button there. At almost the same time, Thread 2 moves that same cursor to position 100,100 because it wants to paint some text. Now several things can happen: Both the button and the text are painted at 10,10 or at 100,100, or the text can be painted at 10,10 and the button can be painted at 100,100. Chances are slim that both will be painted at the right place, but you never know. It's also quite likely that your application will crash horribly.
That's why, in a GUI application, generally one thread paints the screen and receives user events. Any multithreaded application needs safeguards around data that is shared by threads.