]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/thread.h
fix typo in last commit
[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$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
ae93dddf
FM
9/*
10 NOTE: we explicitely don't name wxMutexGUIEnter() and wxMutexGUILeave()
11 as they're not safe. See also ticket #10366.
12*/
13
880efa2a 14/**
36c9828f 15
880efa2a 16@page overview_thread Multithreading
2cd3cc94 17
c6427d4d 18Classes: wxThread, wxThreadHelper, wxMutex, wxCriticalSection, wxCondition, wxSemaphore
2cd3cc94
BP
19
20wxWidgets provides a complete set of classes encapsulating objects necessary in
21multithreaded (MT) programs: the wxThread class itself and different
22synchronization objects: mutexes (see wxMutex) and critical sections (see
c6427d4d 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
ae93dddf
FM
28These classes hopefully make writing MT programs easier and they also
29provide some extra error checking (compared to the native - be it Win32 or
30Posix - thread API), however it is still a non-trivial undertaking especially
31for large projects.
32Before starting an MT application (or starting to add MT
2cd3cc94 33features to an existing one) it is worth asking oneself if there is no easier
ae93dddf
FM
34and safer way to implement the same functionality.
35Of course, in some situations threads really make sense (classical example is a
36server application which launches a new thread for each new client), but in others
37it might be an overkill.
38On the other hand, the recent evolution of the computer hardware shows
c6427d4d
FM
39an important trend towards multi-core systems, which are better exploited using
40multiple threads (e.g. you may want to split a long task among as many threads
41as many CPU (cores) the system reports; see wxThread::GetCPUCount).
42
ae93dddf
FM
43To implement non-blocking operations @e without using multiple threads you have
44two possible implementation choices:
45- use wxIdleEvent (e.g. to perform a long calculation while updating a progress dialog)
46- do everything at once but call wxWindow::Update() or wxApp::YieldFor(wxEVT_CATEGORY_UI)
47 periodically to update the screen.
c6427d4d 48
ae93dddf
FM
49If instead you choose to use threads in your application, it is strongly recommended
50that <b>no secondary threads call GUI functions</b>.
c6427d4d
FM
51The design which uses one GUI thread and several worker threads which communicate
52with the main one using @b events is much more robust and will undoubtedly save you
53countless problems (example: under Win32 a thread can only access GDI objects such
54as pens, brushes, c created by itself and not by the other threads).
2cd3cc94
BP
55
56For communication between secondary threads and the main thread, you may use
36a2d2c4 57wxEvtHandler::QueueEvent or its short version ::wxQueueEvent. These functions
2cd3cc94
BP
58have a thread-safe implementation so that they can be used as they are for
59sending events from one thread to another. However there is no built in method
60to send messages to the worker threads and you will need to use the available
61synchronization classes to implement the solution which suits your needs
62yourself. In particular, please note that it is not enough to derive
63your class from wxThread and wxEvtHandler to send messages to it: in fact, this
64does not work at all.
36c9828f 65
c6427d4d
FM
66See also the @sample{thread} for a sample showing some simple interactions
67between the main and secondary threads.
68
d230488b 69*/
36c9828f 70