]>
Commit | Line | Data |
---|---|---|
15b6757b | 1 | ///////////////////////////////////////////////////////////////////////////// |
d230488b | 2 | // Name: thread.h |
15b6757b FM |
3 | // Purpose: topic overview |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /*! | |
36c9828f | 10 | |
75b31b23 | 11 | @page overview_thread Multithreading overview |
36c9828f | 12 | |
d230488b | 13 | Classes: #wxThread, #wxMutex, #wxCriticalSection, #wxCondition |
75b31b23 | 14 | |
15b6757b | 15 | wxWidgets provides a complete set of classes encapsulating objects necessary in |
d230488b FM |
16 | multithreaded (MT) programs: the wxThread class itself and different |
17 | synchronization objects: mutexes (see wxMutex) and critical sections (see | |
18 | wxCriticalSection) with conditions (see wxCondition). The thread API in wxWidgets | |
19 | resembles to POSIX1.c threads API (a.k.a. pthreads), although several functions have | |
20 | different names and some features inspired by Win32 thread API are there as well. | |
75b31b23 | 21 | |
15b6757b FM |
22 | These classes will hopefully make writing MT programs easier and they also |
23 | provide some extra error checking (compared to the native (be it Win32 or Posix) | |
24 | thread API), however it is still a non-trivial undertaking especially for large | |
25 | projects. Before starting an MT application (or starting to add MT features to | |
26 | an existing one) it is worth asking oneself if there is no easier and safer way | |
27 | to implement the same functionality. Of course, in some situations threads | |
28 | really make sense (classical example is a server application which launches a | |
29 | new thread for each new client), but in others it might be a very poor choice | |
30 | (example: launching a separate thread when doing a long computation to show a | |
31 | progress dialog). Other implementation choices are available: for the progress | |
d230488b FM |
32 | dialog example it is far better to do the calculations in the idle handler |
33 | (see @ref wxIdleEvent) or even simply do everything at once but call wxWindow::Update() | |
34 | periodically to update the screen. | |
75b31b23 | 35 | |
15b6757b FM |
36 | If you do decide to use threads in your application, it is strongly recommended |
37 | that no more than one thread calls GUI functions. The thread sample shows that | |
38 | it @e is possible for many different threads to call GUI functions at once | |
39 | (all the threads created in the sample access GUI), but it is a very poor design | |
40 | choice for anything except an example. The design which uses one GUI thread and | |
41 | several worker threads which communicate with the main one using events is much | |
42 | more robust and will undoubtedly save you countless problems (example: under | |
43 | Win32 a thread can only access GDI objects such as pens, brushes, c created by | |
44 | itself and not by the other threads). | |
75b31b23 | 45 | |
36c9828f | 46 | For communication between secondary threads and the main thread, you may use |
d230488b | 47 | wxEvtHandler::AddPendingEvent or its short version #wxPostEvent. These functions |
15b6757b FM |
48 | have a thread-safe implementation so that they can be used as they are for |
49 | sending events from one thread to another. However there is no built in method | |
50 | to send messages to the worker threads and you will need to use the available | |
51 | synchronization classes to implement the solution which suits your needs | |
52 | yourself. In particular, please note that it is not enough to derive | |
d230488b FM |
53 | your class from #wxThread and #wxEvtHandler to send messages to it: |
54 | in fact, this does not work at all. | |
36c9828f | 55 | |
d230488b | 56 | */ |
36c9828f | 57 |