File: overview_thread.html

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (83 lines) | stat: -rw-r--r-- 10,473 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: Multithreading Overview</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
 <tbody>
 <tr>
  <td id="projectlogo">
    <a href="http://www.wxwidgets.org/" target="_new">
      <img alt="wxWidgets" src="logo.png"/>
    </a>
  </td>
  <td style="padding-left: 0.5em; text-align: right;">
   <span id="projectnumber">Version: 3.0.2</span>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Categories</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Multithreading Overview </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_thread_notes">Important Notes for Multi-threaded Applications</a></li>
</ul>
</div>
<div class="textblock"><p>wxWidgets provides a complete set of classes encapsulating objects necessary in multi-threaded (MT) applications: the <a class="el" href="classwx_thread.html" title="A thread is basically a path of execution through a program.">wxThread</a> class itself and different synchronization objects: mutexes (see <a class="el" href="classwx_mutex.html" title="A mutex object is a synchronization object whose state is set to signaled when it is not owned by any...">wxMutex</a>) and critical sections (see <a class="el" href="classwx_critical_section.html" title="A critical section object is used for exactly the same purpose as a wxMutex.">wxCriticalSection</a>) with conditions (see <a class="el" href="classwx_condition.html" title="wxCondition variables correspond to pthread conditions or to Win32 event objects.">wxCondition</a>).</p>
<p>The thread API in wxWidgets resembles to POSIX1.c threads API (a.k.a. pthreads), although several functions have different names and some features inspired by Win32 thread API are there as well.</p>
<p>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 <a class="el" href="classwx_thread.html#a21ccbc2f91bed8d65aeada49a7f8335d" title="Returns the number of system CPUs or -1 if the value is unknown.">wxThread::GetCPUCount</a>).</p>
<p>To implement non-blocking operations <em>without</em> using multiple threads you have two possible implementation choices:</p>
<ul>
<li>use <a class="el" href="classwx_idle_event.html" title="This class is used for idle events, which are generated when the system becomes idle.">wxIdleEvent</a> (e.g. to perform a long calculation while updating a progress dialog)</li>
<li>do everything at once but call <a class="el" href="classwx_window.html#abaf28f1a075fd1b10f761a8febe597ec" title="Calling this method immediately repaints the invalidated area of the window and all of its children r...">wxWindow::Update()</a> or wxApp::YieldFor(wxEVT_CATEGORY_UI) periodically to update the screen.</li>
</ul>
<p>If instead you choose to use threads in your application, please read the following section of this overview.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="classwx_thread.html" title="A thread is basically a path of execution through a program.">wxThread</a>, <a class="el" href="classwx_thread_helper.html" title="The wxThreadHelper class is a mix-in class that manages a single background thread, either detached or joinable (see wxThread for the differences).">wxThreadHelper</a>, <a class="el" href="classwx_mutex.html" title="A mutex object is a synchronization object whose state is set to signaled when it is not owned by any...">wxMutex</a>, <a class="el" href="classwx_critical_section.html" title="A critical section object is used for exactly the same purpose as a wxMutex.">wxCriticalSection</a>, <a class="el" href="classwx_condition.html" title="wxCondition variables correspond to pthread conditions or to Win32 event objects.">wxCondition</a>, <a class="el" href="classwx_semaphore.html" title="wxSemaphore is a counter limiting the number of threads concurrently accessing a shared resource...">wxSemaphore</a></dd></dl>
<h1><a class="anchor" id="overview_thread_notes"></a>
Important Notes for Multi-threaded Applications</h1>
<p>When writing a multi-threaded application, it is strongly recommended that <b>no secondary threads call GUI functions</b>. The design which uses one GUI thread and several worker threads which communicate with the main one using <b>events</b> 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).</p>
<p>For communication between secondary threads and the main thread, you may use <a class="el" href="classwx_evt_handler.html#acffd03bf407a856166ea71ef0318b59a" title="Queue event for a later processing.">wxEvtHandler::QueueEvent</a> or its short version <a class="el" href="group__group__funcmacro__events.html#gae921d7bd0e52fedbf3f253d2c408bce1" title="Queue an event for processing on the given object.">wxQueueEvent</a>. 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 synchronization classes to implement the solution which suits your needs yourself. In particular, please note that it is not enough to derive your class from <a class="el" href="classwx_thread.html" title="A thread is basically a path of execution through a program.">wxThread</a> and <a class="el" href="classwx_evt_handler.html" title="A class that can handle events from the windowing system.">wxEvtHandler</a> to send messages to it: in fact, this does not work at all. You're instead encouraged to use <a class="el" href="classwx_thread_helper.html" title="The wxThreadHelper class is a mix-in class that manages a single background thread, either detached or joinable (see wxThread for the differences).">wxThreadHelper</a> as it greatly simplifies the communication and the sharing of resources.</p>
<p>You should also look at the <a class="el" href="classwx_thread.html" title="A thread is basically a path of execution through a program.">wxThread</a> docs for important notes about secondary threads and their deletion.</p>
<p>Last, remember that if <a class="el" href="classwx_event_loop_base.html#afb5ed08160c7e6b1364170af6115af55" title="Works like Yield() with onlyIfNeeded == true, except that it allows the caller to specify a mask of t...">wxEventLoopBase::YieldFor()</a> is used directly or indirectly (e.g. through <a class="el" href="classwx_progress_dialog.html" title="If supported by the platform this class will provide the platform&#39;s native progress dialog...">wxProgressDialog</a>) in your code, then you may have both re-entrancy problems and also problems caused by the processing of events out of order. To resolve the last problem <a class="el" href="classwx_thread_event.html" title="This class adds some simple functionality to wxEvent to facilitate inter-thread communication.">wxThreadEvent</a> can be used: thanks to its implementation of the <a class="el" href="classwx_thread_event.html#a21a87c9ffaa750f3daf05fb628fe9251" title="Returns wxEVT_CATEGORY_THREAD.">wxThreadEvent::GetEventCategory</a> function <a class="el" href="classwx_thread_event.html" title="This class adds some simple functionality to wxEvent to facilitate inter-thread communication.">wxThreadEvent</a> classes in fact do not get processed by <a class="el" href="classwx_event_loop_base.html#afb5ed08160c7e6b1364170af6115af55" title="Works like Yield() with onlyIfNeeded == true, except that it allows the caller to specify a mask of t...">wxEventLoopBase::YieldFor()</a> unless you specify the <code>wxEVT_CATEGORY_THREAD</code> flag.</p>
<p>See also the <a class="el" href="page_samples.html#page_samples_thread">Thread Sample</a> for a sample showing some simple interactions between the main and secondary threads. </p>
</div></div><!-- contents -->

<address class="footer">
	<small>
		Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
	</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>