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