Hardware Reference
In-Depth Information
Notice that the header file for
signal.h
is needed for the function prototype and the
signal definitions.
thread
: This is the thread ID that you want to signal (or test).
sig
: This is the signal that you wish to send. Alternatively,
supply zero to test whether the thread exists.
returns
: Returns zero if the call is successful, or an error code
(not in
errno
).
Error
Description
EINVAL
An invalid signal was specified.
ESRCH
No thread with the ID thread could be found.
One useful application of the
pthread_kill(3)
function is to test whether another
thread exists. If the
sig
argument is supplied with zero, no actual signal is delivered, but
the error checking is still performed. If the function returns zero, you know that the thread
still
exists
.
But what does it mean when the thread exists? Does it mean that it is still
executing
?
Or does it mean that it has not been reclaimed as part of a
pthread_join(3)
, or as a
consequence of
pthread_detach(3)
cleanup?
It turns out that when the thread
exists
, it means that it is still executing. In other
words, it
has not returned
from the thread function that was started. If the thread has
returned, it is considered to be incapable of receiving a signal.
Based on this, you know that you will get a zero returned when the thread is still
executing. When error code
ESRCH
is returned instead, you know that the thread has
completed.
Mutexes
While not strictly a CPU topic, mutexes cannot be separated from a discussion on threads.
A
mutex
is a locking device that allows the software designer to stop one or more threads
while another is working with a shared resource. In other words, one thread receives
exclusive access. This is necessary to facilitate inter-thread communication. I'm simply going
to describe the mutex API here, rather than the theory behind the application of mutexes.
pthread_mutex_create(3)
A mutex is initialized with the system call to
pthread_mutex_init(3)
:
int pthread_mutex_init(
pthread_mutex_t
mutex,
∗
const pthread_mutexattr_t
attr
∗
);