Newer
Older
monitord / jthread-1.2.1 / doc / .svn / text-base / manual.tex.svn-base
  1. \documentclass[a4paper,12pt]{article}
  2. \usepackage{listings}
  3. \lstset{language=C++}
  4. \lstset{tabsize=4}
  5. \begin{document}
  6. \title{JThread manual (v1.2.1)}
  7. \author{Jori Liesenborgs\\
  8. {\tt jori.liesenborgs@gmail.com} }
  9. \date{June 20, 2006}
  10. \maketitle
  11.  
  12. \section{Introduction}
  13.  
  14. A lot of projects on which I'm working use threads. To be able to
  15. use the same code on both unix and MS-Windows platforms, I decided
  16. to write some simple wrapper classes for the existing thread functions
  17. on those platforms.
  18.  
  19. The JThread package is very simple: currently, it only contains three
  20. classes, namely {\tt JThread}, {\tt JMutex} and {\tt JMutexAutoLock}.
  21. As their names might
  22. suggest, {\tt JThread} represents a thread and {\tt JMutex} a mutex.
  23. The thread class only contains very basic functions, for example to
  24. start or kill a thread.
  25.  
  26. \section{Copyright \& disclaimer}
  27.  
  28. Permission is hereby granted, free of charge, to any person obtaining a
  29. copy of this software and associated documentation files (the "Software"),
  30. to deal in the Software without restriction, including without limitation
  31. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  32. and/or sell copies of the Software, and to permit persons to whom the
  33. Software is furnished to do so, subject to the following conditions:
  34.  
  35. The above copyright notice and this permission notice shall be included
  36. in all copies or substantial portions of the Software.
  37.  
  38. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  39. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  41. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  43. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  44. IN THE SOFTWARE.
  45.  
  46. \section{Usage}
  47.  
  48. Here follows a description of the {\tt JThread}, {\tt JMutex} and
  49. {\tt JMutexAutoLock} classes.
  50. Note that functions with return type {\tt int} always return a value of zero
  51. or more on success and a negative value in case something went wrong.
  52.  
  53. \subsection{{\tt JMutex}}
  54.  
  55. The class definition of {\tt JMutex} is shown below. Before you can use an
  56. instance of this type, you must first call the {\tt Init} function. You can
  57. check if the mutex was already initialized by checking the return value
  58. of {\tt IsInitialized}. After the initialization, the mutex can be locked
  59. and unlocked by calling the functions {\tt Lock} and {\tt Unlock} respectively.
  60.  
  61. \begin{lstlisting}[frame=tb]{}
  62. class JMutex
  63. {
  64. public:
  65. JMutex();
  66. ~JMutex();
  67. int Init();
  68. int Lock();
  69. int Unlock();
  70. bool IsInitialized();
  71. };
  72. \end{lstlisting}
  73.  
  74. \subsection{{\tt JMutexAutoLock}}
  75.  
  76. The class definition of {\tt JMutexAutoLock} is shown below. It is meant
  77. to make it easier to implement thread-safe functions, without having to
  78. worry about when to unlock a mutex.
  79.  
  80. \begin{lstlisting}[frame=tb]{}
  81. class JMutexAutoLock
  82. {
  83. public:
  84. JMutexAutoLock(JMutex &m);
  85. ~JMutexAutoLock();
  86. };
  87. \end{lstlisting}
  88.  
  89. The code below illustrates the way this class can be used:
  90.  
  91. \begin{lstlisting}[frame=tb]{}
  92. void MyClass::MyFunction()
  93. {
  94. JMutexAutoLock autoLock(m_myMutex);
  95. // Do operations protected by mutex 'm_myMutex' here
  96. }
  97. \end{lstlisting}
  98. When the {\tt autoLock} variable is created, it automatically locks the
  99. mutex {\tt m\_myMutex} specified in the constructor. The destructor of
  100. the {\tt autoLock} variable makes sure the lock is released again.
  101.  
  102. \subsection{{\tt JThread}}
  103. To create your own thread, you have to derive a class from {\tt JThread},
  104. which is depicted below. In your derived class, you have to implement
  105. a member function {\tt Thread}, which will be executed in the new thread.
  106. Your own {\tt Thread} implementation should call {\tt ThreadStarted}
  107. immediately.
  108.  
  109. To start your thread, you simply have to call the {\tt Start} function.
  110. This function finishes when your own {\tt Thread} function has called
  111. {\tt ThreadStarted}. This way, when the {\tt Start} function
  112. finishes, you can be really sure that your own {\tt Thread} implementation
  113. is really running.
  114.  
  115. You can check if the thread is still running by calling {\tt IsRunning}.
  116. If the thread has finished, you can check its return value by calling
  117. {\tt GetReturnValue}. Finally, in case your thread gets stuck, you can
  118. end it by using the {\tt Kill} function.
  119.  
  120. You should be careful with this {\tt Kill} function: if you call it when
  121. the thread is working with a mutex (for example an internal mutex), this
  122. mutex can be left in a locked state, which in turn can cause another thread
  123. to block. You should only use the {\tt Kill} function when you're absolutely
  124. sure that the thread is stuck in some loop and cannot be ended otherwise.
  125.  
  126. \begin{lstlisting}[frame=tb]{}
  127. class JThread
  128. {
  129. public:
  130. JThread();
  131. virtual ~JThread();
  132. int Start();
  133. int Kill();
  134. virtual void *Thread() = 0;
  135. bool IsRunning();
  136. void *GetReturnValue();
  137. protected:
  138. void ThreadStarted();
  139. };
  140. \end{lstlisting}
  141. \end{document}
  142.