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