manual.tex
5.21 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=C++}
\lstset{tabsize=4}
\begin{document}
\title{JThread manual (v1.3.3)}
\author{Jori Liesenborgs\\
{\tt jori.liesenborgs@gmail.com} }
\date{March 22, 2017}
\maketitle
\section{Introduction}
A lot of projects on which I'm working use threads. To be able to
use the same code on both unix and MS-Windows platforms, I decided
to write some simple wrapper classes for the existing thread functions
on those platforms.
The JThread package is very simple: currently, it only contains three
classes, namely {\tt JThread}, {\tt JMutex} and {\tt JMutexAutoLock}.
As their names might
suggest, {\tt JThread} represents a thread and {\tt JMutex} a mutex.
The thread class only contains very basic functions, for example to
start or kill a thread.
\section{Copyright \& disclaimer}
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
\section{Usage}
Here follows a description of the {\tt JThread}, {\tt JMutex} and
{\tt JMutexAutoLock} classes, which are all in the namespace {\tt jthread}.
Note that functions with return type {\tt int} always return a value of zero
or more on success and a negative value in case something went wrong.
\subsection{{\tt JMutex}}
The class definition of {\tt JMutex} is shown below. Before you can use an
instance of this type, you must first call the {\tt Init} function. You can
check if the mutex was already initialized by checking the return value
of {\tt IsInitialized}. After the initialization, the mutex can be locked
and unlocked by calling the functions {\tt Lock} and {\tt Unlock} respectively.
\begin{lstlisting}[frame=tb]{}
class JMutex
{
public:
JMutex();
~JMutex();
int Init();
int Lock();
int Unlock();
bool IsInitialized();
};
\end{lstlisting}
\subsection{{\tt JMutexAutoLock}}
The class definition of {\tt JMutexAutoLock} is shown below. It is meant
to make it easier to implement thread-safe functions, without having to
worry about when to unlock a mutex.
\begin{lstlisting}[frame=tb]{}
class JMutexAutoLock
{
public:
JMutexAutoLock(JMutex &m);
~JMutexAutoLock();
};
\end{lstlisting}
The code below illustrates the way this class can be used:
\begin{lstlisting}[frame=tb]{}
void MyClass::MyFunction()
{
JMutexAutoLock autoLock(m_myMutex);
// Do operations protected by mutex 'm_myMutex' here
}
\end{lstlisting}
When the {\tt autoLock} variable is created, it automatically locks the
mutex {\tt m\_myMutex} specified in the constructor. The destructor of
the {\tt autoLock} variable makes sure the lock is released again.
\subsection{{\tt JThread}}
To create your own thread, you have to derive a class from {\tt JThread},
which is depicted below. In your derived class, you have to implement
a member function {\tt Thread}, which will be executed in the new thread.
Your own {\tt Thread} implementation should call {\tt ThreadStarted}
immediately.
To start your thread, you simply have to call the {\tt Start} function.
This function finishes when your own {\tt Thread} function has called
{\tt ThreadStarted}. This way, when the {\tt Start} function
finishes, you can be really sure that your own {\tt Thread} implementation
is really running.
You can check if the thread is still running by calling {\tt IsRunning}.
If the thread has finished, you can check its return value by calling
{\tt GetReturnValue}. Finally, in case your thread gets stuck, you can
end it by using the {\tt Kill} function.
You should be careful with this {\tt Kill} function: if you call it when
the thread is working with a mutex (for example an internal mutex), this
mutex can be left in a locked state, which in turn can cause another thread
to block. You should only use the {\tt Kill} function when you're absolutely
sure that the thread is stuck in some loop and cannot be ended otherwise.
With the {\tt IsSameThread} function you can check if the thread of the
caller is the same as the thread that was started and is executing the
{\tt Thread} function.
\begin{lstlisting}[frame=tb]{}
class JThread
{
public:
JThread();
virtual ~JThread();
int Start();
int Kill();
virtual void *Thread() = 0;
bool IsRunning();
void *GetReturnValue();
bool IsSameThread();
protected:
void ThreadStarted();
};
\end{lstlisting}
\end{document}