⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 developer.htm

📁 A class that allows the simple implementation of worker threads
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<p class=MsoNormal style='text-align:justify'>&nbsp;</p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![endif]><o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span><font size="4"><a name="CThread_Class_Conception"><b><span style='mso-bookmark:CThread_Class_Conception'>CThread Class
Conception</span></b></a></font></p>

<p style='text-align:justify'><a name="Thread_Task_Paradigms"><span style='mso-bookmark:Thread_Task_Paradigms'><font size="4">Thread-Task
Paradigms</font></span></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><b
style='mso-bidi-font-weight:normal'><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'>CThread</span></b><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'> abstract class defines conception describing the main requirements
regarding the thread handling. There are two main paradigms concerning thread task
implementation:<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]><![endif]><o:p></o:p></span></p>

<p style='text-align:justify'><a name="Trivial_Threads"><font size="4">Trivial Threads</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>Thread task is a simple
sequence of commands that are to be done. After starting the thread, the thread
terminates after completing the whole task. Owner thread (typically the main
application thread) may communicate with a CThread thread by some intermediate
object visible for both sides. This communication, however, does not provide
effective parent-child-thread notifications. Maintaining all risky situations
originating in such communication requires an additional developer's effort.
Using of this conception is recommended for linear or straightforward
heavyweight tasks, more or less independent from the owner thread that do not
require sophisticated or intensive communication with the owner thread. CThread
thread supporting this paradigm is called <i style='mso-bidi-font-style:normal'>Trivial
Thread</i>.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]><![endif]><o:p></o:p></span></p>

<p style='text-align:justify'><a name="Notificable_Threads"><font size="4">Notificable
Threads</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>In opposite to <i
style='mso-bidi-font-style:normal'>Trivial Threads</i>, the thread task may be
application-sensitive and listens to the owner thread commands. In this case
the thread task is a loop in which thread waits for incoming notifications
(commands). After receiving a command the thread executes this command.
Incoming commands are handled sequentially in the thread task procedure.
Simultaneously, the thread may set the current thread activity status to inform
the owner thread.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>Notificable Threads act as
'schedulers' or 'services'. That means, these threads execute their task on the
owner thread demand and wait for another command. Usually the task executed in
such thread should not be too long to allow an owner thread to make an
effective controlling over the thread. This thread is called <i
style='mso-bidi-font-style:normal'>Notificable Thread</i>.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]>&nbsp;<![endif]><o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><b style='mso-bidi-font-weight:
normal'><span style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>CThread</span></b><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'> class supports both
paradigms but emphasizes developers to use <i style='mso-bidi-font-style:normal'>Notificable
Threads</i>.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]><![endif]><o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'>&nbsp;</p>

<p style='text-align:justify'><font size="4"><a name="Thread_Synchronization"><b><span style='mso-bookmark:Thread_Synchronization'>Thread
Synchronization</span></b></a></font></p>

<p style='text-align:justify'><a name="Thread_Handler_Oriented_Synchronization"><font size="4">Thread-Handler-Oriented Synchronization</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><b
style='mso-bidi-font-weight:normal'><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'>CThread</span></b><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'> derived classes may utilize the special synchronization feature that
is implemented in the basic <b style='mso-bidi-font-weight:normal'>CThread</b>
class. The mentioned <i style='mso-bidi-font-style:normal'>Thread-Handler-Oriented
Synchronization</i> is a powerful feature provided by <b style='mso-bidi-font-weight:
normal'>CThread</b> class. Developers do not have to deal too much with
synchronization among thread objects using the same thread-task handler (the <b
style='mso-bidi-font-weight:normal'>ThreadHandler()</b> method). They just use <b
style='mso-bidi-font-weight:normal'>Lock()</b> or <b style='mso-bidi-font-weight:
normal'>Unlock()</b> <b style='mso-bidi-font-weight:normal'>CThread</b> methods
to lock the critical code that is to be executed exclusively in the <b
style='mso-bidi-font-weight:normal'>ThreadHandler()</b> method. Developers may,
however, omit this synchronization feature and define the <b style='mso-bidi-font-weight:
normal'>CThread</b> derived class, which does not support this kind of
synchronization. It is up to the developer抯 responsibility to implement
non-critical thread task or instantiate just one thread object in such case.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>Each <b style='mso-bidi-font-weight:
normal'>CThread</b> derived class requiring its own <i style='mso-bidi-font-style:
normal'>Thread-Handler-Oriented Synchronization</i> must declare this feature
explicitly (this can be automatically established while working with <i
style='mso-bidi-font-style:normal'>Worker Thread Class Generator</i> Wizard).<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>This kind of synchronization
is so-called <i style='mso-bidi-font-style:normal'>Thread-Handler-Oriented.</i>
Suppose a developer utilizes two <b style='mso-bidi-font-weight:normal'>CThread</b>
derived classes <b style='mso-bidi-font-weight:normal'>CThreadDerived1</b> and <b
style='mso-bidi-font-weight:normal'>CThreadDerived2</b> and each class
implements its own <b style='mso-bidi-font-weight:normal'>ThreadHandler()</b>
method. Let <i style='mso-bidi-font-style:normal'>Thread11</i> and <i
style='mso-bidi-font-style:normal'>Thread12</i> are the instantiated objects of
the class <b style='mso-bidi-font-weight:normal'>CThreadDerived1</b> and <i
style='mso-bidi-font-style:normal'>Thread21</i> and <i style='mso-bidi-font-style:
normal'>Thread22</i> are the objects of the class <b style='mso-bidi-font-weight:
normal'>CThreadDerived2</b>. <i style='mso-bidi-font-style:normal'>Thread11</i>
is synchronized with <i style='mso-bidi-font-style:normal'>Thread12</i> but not
with <i style='mso-bidi-font-style:normal'>Thread21</i> or <i style='mso-bidi-font-style:
normal'>Thread22</i> and vice versa. All thread objects of the <b
style='mso-bidi-font-weight:normal'>CThreadDerived1</b> class are synchronized
among each other (as well as all objects of the <b style='mso-bidi-font-weight:
normal'>CThreadDerived2</b> class) but the synchronization of <b
style='mso-bidi-font-weight:normal'>CThreadDerived1</b> objects is fully
independent from the synchronization of <b style='mso-bidi-font-weight:normal'>CThreadDerived2</b>
objects.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>On the other hand, if the <b
style='mso-bidi-font-weight:normal'>CThreadDerived3</b> class is derived from
the <b style='mso-bidi-font-weight:normal'>CThreadDerived2</b> but do not
implement its own <b style='mso-bidi-font-weight:normal'>ThreadHandler()</b>
method (uses the handler implemented in the <b style='mso-bidi-font-weight:
normal'>CThreadDerived2</b> class), the <b style='mso-bidi-font-weight:normal'>CThreadDerived3</b>
objects are automatically synchronized with the <b style='mso-bidi-font-weight:
normal'>CThreadDerived2</b> objects and vice versa.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>In a <b style='mso-bidi-font-weight:
normal'>CThread</b> object-oriented hierarchy developers may design several <b
style='mso-bidi-font-weight:normal'>CThread</b>-derived classes for different
purposes. Some of them will implement the specific thread-task handlers the
others will inherit them. <i style='mso-bidi-font-style:normal'>Thread-Handler-Oriented
Synchronization</i> allows developers to split <b style='mso-bidi-font-weight:
normal'>CThread</b> objects into the groups that contain <b style='mso-bidi-font-weight:
normal'>CThread</b>-derived objects operating on the same thread-task handler.
Thus, each group defines its own thread-task handler and logically provides the
synchronization for all objects belonging to this group. On the other hand,
another group operates on a different thread-task handler, executing a
completely different task independent from another group, so the
synchronization has also to be independent from another group. <i
style='mso-bidi-font-style:normal'>Thread-Handler-Oriented Synchronization</i>
establishes and maintains the independent synchronization for each group
automatically. All the developer has to do is to write the
SUPPORT_THREAD_SYNCHRONIZATION(<i style='mso-bidi-font-style:normal'>ClassName</i>)
macro in the constructor in his <b style='mso-bidi-font-weight:normal'>CThread</b>-derived
<i style='mso-bidi-font-style:normal'>ClassName</i> class where the thread-task
handler (the virtual <b style='mso-bidi-font-weight:normal'>ThreadHandler()</b>
method) is actually implemented. <i style='mso-bidi-font-style:normal'>Thread-Handler-Oriented
Synchronization</i> is supported for both <i style='mso-bidi-font-style:normal'>Trivial</i>
and <i style='mso-bidi-font-style:normal'>Notificable</i> <b style='mso-bidi-font-weight:
normal'>CThread</b> <i style='mso-bidi-font-style:normal'>Threads</i>.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]><![endif]><o:p></o:p></span></p>

<p style='text-align:justify'><a name="Single_Thread_Object_Synchronization"><font size="4">Single Thread
Object Synchronization</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><b
style='mso-bidi-font-weight:normal'><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'>CThread</span></b><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'> class itself implements an <i style='mso-bidi-font-style:normal'>internal
synchronization</i>. This is the special synchronization feature that is not
visible from within the owner thread. Internal synchronization does not impact <b
style='mso-bidi-font-weight:normal'>Lock()</b> or <b style='mso-bidi-font-weight:
normal'>Unlock()</b> mechanism mentioned in the above paragraph under any
circumstances. The benefit of the internal synchronization is a synchronized
access to critical <b style='mso-bidi-font-weight:normal'>CThread</b> methods
while accessing the one instance of <b style='mso-bidi-font-weight:normal'>CThread</b>
object asynchronously.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>This kind of synchronization
is sometimes called <i style='mso-bidi-font-style:normal'>Single Thread Object
Synchronization</i>. If, for example, the <b style='mso-bidi-font-weight:normal'>childThread</b>
is an instance of <b style='mso-bidi-font-weight:normal'>CThread</b>-derived
class, which is shared by two other (arbitrary) threads <b style='mso-bidi-font-weight:
normal'>parentThread1 </b>and <b style='mso-bidi-font-weight:normal'>parentThread2</b>,
both these parent threads operate on the <b style='mso-bidi-font-weight:normal'>childThread</b>
object asynchronously. The internal synchronization implemented in <b
style='mso-bidi-font-weight:normal'>CThread</b> class guarantees that all
critical <b style='mso-bidi-font-weight:normal'>childThread</b> methods will be
executed properly regardless the parent thread owning the current <b
style='mso-bidi-font-weight:normal'>childThread</b> focus. The only care that
must be taken is that none of the parent threads deletes the <b
style='mso-bidi-font-weight:normal'>childThread</b>'s <b style='mso-bidi-font-weight:
normal'>CThread</b> object while another is still operating on it.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>Despite the given guaranty
there is one situation where the concurrent operating on the same object may be
confusing. It concerns mainly <i style='mso-bidi-font-style:normal'>Notificable
Threads</i> and sending commands from the parent threads to the <b
style='mso-bidi-font-weight:normal'>childThread</b>. If both parent threads
send several commands asynchronously to the <b style='mso-bidi-font-weight:
normal'>childThread</b> there is no any guaranty about which command will be actually
handled in the <b style='mso-bidi-font-weight:normal'>childThread</b> at a
moment. The similar situation may occur also in <i style='mso-bidi-font-style:
normal'>Trivial Threads</i> while setting up some important controlling object
property (or variable) used in the <b style='mso-bidi-font-weight:normal'>childThread</b>
by both parent threads concurrently. For this reason, a
concurrency-handling-of-one-object design has to be carefully prepared.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span
style='font-size:11.0pt;mso-bidi-font-size:10.0pt'>This kind of synchronization
is established automatically while registering <b style='mso-bidi-font-weight:
normal'>CThread</b> class in an application process for both <i
style='mso-bidi-font-style:normal'>Trivial</i> and <i style='mso-bidi-font-style:
normal'>Notificable</i> <i style='mso-bidi-font-style:normal'>Threads</i>.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]><![endif]><o:p></o:p></span></p>

<p style='text-align:justify'><a name="Process_Synchronization"><font size="4">Process Synchronization</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

<p class=MsoNormal style='text-align:justify'><b
style='mso-bidi-font-weight:normal'><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'>CThread</span></b><span style='font-size:11.0pt;mso-bidi-font-size:
10.0pt'>-derived class offers also a global locking mechanism, which is
exclusive for the whole process. The user may use <b style='mso-bidi-font-weight:
normal'>ProcessLock()</b> or <b style='mso-bidi-font-weight:normal'>ProcessUnlock()</b>
<b style='mso-bidi-font-weight:normal'>CThread</b> static methods (that were
previously opened by <b style='mso-bidi-font-weight:normal'>OpenProcessLocking()</b>
method) wherever in a code. These methods are static so there is no necessary
to instantiate any <b style='mso-bidi-font-weight:normal'>CThread</b> object.
User may use this synchronization mechanism to accomplish an exclusive access
to the global-critical resources (opening the file, common communication
object, singleton etc.). <i style='mso-bidi-font-style:normal'>Process
Synchronization</i> may be used in an arbitrary part of the code not
necessarily in <b style='mso-bidi-font-weight:normal'>CThread</b> tasks only.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'>The mentioned synchronization does not support an
inter-process synchronization.<o:p></o:p></span></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]>&nbsp;<![endif]><o:p></o:p></span></p>

<p style='text-align:justify'><a name="Thread_Notification"><font size="4">Thread Notification</font></a></p>

<p class=MsoNormal style='text-align:justify'><span style='font-size:11.0pt;
mso-bidi-font-size:10.0pt'><![if !supportEmptyParas]></span></p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -