From 41fc16489e598159f050aaaf2c0f7db7b6b6efe7 Mon Sep 17 00:00:00 2001 From: PB Date: Tue, 1 Nov 2022 18:19:55 +0100 Subject: [PATCH] Update Multithreading Overview programming guide --- docs/doxygen/overviews/thread.h | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/docs/doxygen/overviews/thread.h b/docs/doxygen/overviews/thread.h index fcbb8895dc..ba78317804 100644 --- a/docs/doxygen/overviews/thread.h +++ b/docs/doxygen/overviews/thread.h @@ -16,26 +16,21 @@ @tableofcontents +@note In the new code, it is highly recommended to use concurrency classes + provided in C++11 and newer, instead of their wxWidgets counterparts. + The warning about not using GUI classes from non-GUI threads still applies. + wxWidgets provides a complete set of classes encapsulating objects necessary in multi-threaded (MT) applications: the wxThread class itself and different synchronization objects: mutexes (see wxMutex) and critical sections (see wxCriticalSection) with conditions (see wxCondition). The thread API in -wxWidgets resembles to POSIX1.c threads API (a.k.a. pthreads), although several +wxWidgets resembles to POSIX thread API (a.k.a. pthreads), although several functions have different names and some features inspired by Win32 thread API are there as well. These classes hopefully make writing MT programs easier and they also provide some extra error checking (compared to the native - be it Win32 or Posix - -thread API), however it is still a non-trivial undertaking especially for large -projects. Before starting an MT application (or starting to add MT features to -an existing one) it is worth asking oneself if there is no easier and safer way -to implement the same functionality. Of course, in some situations threads -really make sense (classical example is a server application which launches a -new thread for each new client), but in others it might be an overkill. On the -other hand, the recent evolution of the computer hardware shows an important -trend towards multi-core systems, which are better exploited using multiple -threads (e.g. you may want to split a long task among as many threads as many -CPU (cores) the system reports; see wxThread::GetCPUCount). +thread API). To implement non-blocking operations @e without using multiple threads you have two possible implementation choices: @@ -44,8 +39,8 @@ two possible implementation choices: - do everything at once but call wxWindow::Update() or wxApp::YieldFor(wxEVT_CATEGORY_UI) periodically to update the screen. -If instead you choose to use threads in your application, please read the -following section of this overview. +However, it is generally much better to run time-consuming tasks in worker threads instead +of trying to work around blocked GUI (and risk reentrancy problems). @see wxThread, wxThreadHelper, wxMutex, wxCriticalSection, wxCondition, wxSemaphore @@ -60,9 +55,11 @@ thread and several worker threads which communicate with the main one using @b events is much more robust and will undoubtedly save you countless problems (example: under Win32 a thread can only access GDI objects such as pens, brushes, device contexts created by itself and not by the other threads). +The GUI thread is the thread in which wxWidgets was initialized, where +wxIsMainThread() returns @true. For communication between secondary threads and the main thread, you may use -wxEvtHandler::QueueEvent or its short version ::wxQueueEvent. These functions +wxEvtHandler::QueueEvent() or its short version ::wxQueueEvent(). These functions have a thread-safe implementation so that they can be used as they are for sending events from one thread to another. However there is no built in method to send messages to the worker threads and you will need to use the available