]>
Commit | Line | Data |
---|---|---|
15b6757b | 1 | ///////////////////////////////////////////////////////////////////////////// |
d230488b | 2 | // Name: thread.h |
15b6757b FM |
3 | // Purpose: topic overview |
4 | // Author: wxWidgets team | |
526954c5 | 5 | // Licence: wxWindows licence |
15b6757b FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
ae93dddf | 8 | /* |
4c51a665 | 9 | NOTE: we explicitly don't name wxMutexGUIEnter() and wxMutexGUILeave() |
ae93dddf FM |
10 | as they're not safe. See also ticket #10366. |
11 | */ | |
12 | ||
880efa2a | 13 | /** |
36c9828f | 14 | |
3a567740 | 15 | @page overview_thread Multithreading Overview |
2cd3cc94 | 16 | |
831e1028 | 17 | @tableofcontents |
3a567740 | 18 | |
2cd3cc94 | 19 | wxWidgets provides a complete set of classes encapsulating objects necessary in |
831e1028 | 20 | multi-threaded (MT) applications: the wxThread class itself and different |
2cd3cc94 | 21 | synchronization objects: mutexes (see wxMutex) and critical sections (see |
831e1028 | 22 | wxCriticalSection) with conditions (see wxCondition). The thread API in |
2cd3cc94 BP |
23 | wxWidgets resembles to POSIX1.c threads API (a.k.a. pthreads), although several |
24 | functions have different names and some features inspired by Win32 thread API | |
25 | are there as well. | |
26 | ||
831e1028 BP |
27 | These classes hopefully make writing MT programs easier and they also provide |
28 | some extra error checking (compared to the native - be it Win32 or Posix - | |
29 | thread API), however it is still a non-trivial undertaking especially for large | |
30 | projects. Before starting an MT application (or starting to add MT features to | |
31 | an existing one) it is worth asking oneself if there is no easier and safer way | |
32 | to implement the same functionality. Of course, in some situations threads | |
33 | really make sense (classical example is a server application which launches a | |
34 | new thread for each new client), but in others it might be an overkill. On the | |
35 | other hand, the recent evolution of the computer hardware shows an important | |
36 | trend towards multi-core systems, which are better exploited using multiple | |
37 | threads (e.g. you may want to split a long task among as many threads as many | |
38 | CPU (cores) the system reports; see wxThread::GetCPUCount). | |
c6427d4d | 39 | |
ae93dddf FM |
40 | To implement non-blocking operations @e without using multiple threads you have |
41 | two possible implementation choices: | |
831e1028 | 42 | |
ae93dddf FM |
43 | - use wxIdleEvent (e.g. to perform a long calculation while updating a progress dialog) |
44 | - do everything at once but call wxWindow::Update() or wxApp::YieldFor(wxEVT_CATEGORY_UI) | |
45 | periodically to update the screen. | |
3a567740 | 46 | |
831e1028 BP |
47 | If instead you choose to use threads in your application, please read the |
48 | following section of this overview. | |
49 | ||
50 | @see wxThread, wxThreadHelper, wxMutex, wxCriticalSection, wxCondition, | |
51 | wxSemaphore | |
c6427d4d | 52 | |
831e1028 BP |
53 | |
54 | ||
55 | @section overview_thread_notes Important Notes for Multi-threaded Applications | |
56 | ||
57 | When writing a multi-threaded application, it is strongly recommended that | |
58 | <b>no secondary threads call GUI functions</b>. The design which uses one GUI | |
59 | thread and several worker threads which communicate with the main one using | |
60 | @b events is much more robust and will undoubtedly save you countless problems | |
61 | (example: under Win32 a thread can only access GDI objects such as pens, | |
62 | brushes, device contexts created by itself and not by the other threads). | |
2cd3cc94 BP |
63 | |
64 | For communication between secondary threads and the main thread, you may use | |
36a2d2c4 | 65 | wxEvtHandler::QueueEvent or its short version ::wxQueueEvent. These functions |
2cd3cc94 BP |
66 | have a thread-safe implementation so that they can be used as they are for |
67 | sending events from one thread to another. However there is no built in method | |
68 | to send messages to the worker threads and you will need to use the available | |
69 | synchronization classes to implement the solution which suits your needs | |
831e1028 BP |
70 | yourself. In particular, please note that it is not enough to derive your class |
71 | from wxThread and wxEvtHandler to send messages to it: in fact, this does not | |
72 | work at all. You're instead encouraged to use wxThreadHelper as it greatly | |
73 | simplifies the communication and the sharing of resources. | |
3a567740 FM |
74 | |
75 | You should also look at the wxThread docs for important notes about secondary | |
76 | threads and their deletion. | |
77 | ||
831e1028 BP |
78 | Last, remember that if wxEventLoopBase::YieldFor() is used directly or |
79 | indirectly (e.g. through wxProgressDialog) in your code, then you may have both | |
80 | re-entrancy problems and also problems caused by the processing of events out | |
81 | of order. To resolve the last problem wxThreadEvent can be used: thanks to its | |
82 | implementation of the wxThreadEvent::GetEventCategory function wxThreadEvent | |
83 | classes in fact do not get processed by wxEventLoopBase::YieldFor() unless you | |
84 | specify the @c wxEVT_CATEGORY_THREAD flag. | |
36c9828f | 85 | |
c6427d4d FM |
86 | See also the @sample{thread} for a sample showing some simple interactions |
87 | between the main and secondary threads. | |
88 | ||
d230488b | 89 | */ |