]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/thread.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_thread Multithreading
13 Classes: wxThread, wxThreadHelper, wxMutex, wxCriticalSection, wxCondition, wxSemaphore
15 wxWidgets provides a complete set of classes encapsulating objects necessary in
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
19 wxWidgets resembles to POSIX1.c threads API (a.k.a. pthreads), although several
20 functions have different names and some features inspired by Win32 thread API
23 These classes will hopefully make writing MT programs easier and they also
24 provide some extra error checking (compared to the native (be it Win32 or
25 Posix) thread API), however it is still a non-trivial undertaking especially
26 for large projects. Before starting an MT application (or starting to add MT
27 features to an existing one) it is worth asking oneself if there is no easier
28 and safer way to implement the same functionality. Of course, in some
29 situations threads really make sense (classical example is a server application
30 which launches a new thread for each new client), but in others it might be an
31 overkill. On the other hand, the recent evolution of the computer hardware shows
32 an important trend towards multi-core systems, which are better exploited using
33 multiple threads (e.g. you may want to split a long task among as many threads
34 as many CPU (cores) the system reports; see wxThread::GetCPUCount).
36 To implement non-blocking operations without using multiple threads you have
37 two other possible implementation choices:
38 - using wxIdleEvent (e.g. to perform a long calculation while updating a progress dialog)
39 - simply do everything at once but call wxWindow::Update() periodically to update the screen.
41 Even if there are the ::wxMutexGuiEnter and ::wxMutexGuiLeave functions which allows
42 to use GUI functions from multiple threads, if you do decide to use threads in your
43 application, it is strongly recommended that <b>no more than one calls GUI functions</b>.
44 The design which uses one GUI thread and several worker threads which communicate
45 with the main one using @b events is much more robust and will undoubtedly save you
46 countless problems (example: under Win32 a thread can only access GDI objects such
47 as pens, brushes, c created by itself and not by the other threads).
49 For communication between secondary threads and the main thread, you may use
50 wxEvtHandler::QueueEvent or its short version ::wxQueueEvent. These functions
51 have a thread-safe implementation so that they can be used as they are for
52 sending events from one thread to another. However there is no built in method
53 to send messages to the worker threads and you will need to use the available
54 synchronization classes to implement the solution which suits your needs
55 yourself. In particular, please note that it is not enough to derive
56 your class from wxThread and wxEvtHandler to send messages to it: in fact, this
59 See also the @sample{thread} for a sample showing some simple interactions
60 between the main and secondary threads.