Hardware Reference
In-Depth Information
arg
: This generic pointer is passed to
start_routine
. It may
point to anything of interest to the thread function
(
start_routine
). Often this is a structure containing values,
or in a C++ program, it can be the pointer to an object. If you
don't need an argument value, supply zero (or
NULL
).
returns
: Zero is returned if the function is successful;
otherwise, an error number is returned (not in
errno
).
Error
Description
EAGAIN
Insufficient resources to create another thread, or a system-imposed limit
on the number of threads was encountered.
EINVAL
Invalid settings in attr.
EPERM
No permission to set the scheduling policy and parameters specified in attr.
The C language syntax of argument 3 is a bit nasty for beginning C programmers.
Let's just show what the function for argument 3 looks like:
void
∗
start_routine(void
arg) {
∗
...
return some_ptr;
}
The following is perhaps the simplest example of thread creation possible:
static void
∗
my_thread(void
arg) {
... // thread execution
return 0;
}
∗
int
main(int argc, char
argv) {
pthread_t tid; // Thread ID
int rc;
∗∗
rc = pthread_create(&tid,0,my_thread,0);
assert(!rc);
This example does not use thread attributes (argument 2 is zero). We also don't care
about the value passed into
my_thread()
, so argument 4 is provided a zero. Argument 3
simply needs to tell the system call what function to execute. The value of
rc
will be zero if
the thread is successfully created (tested by the
assert(3)
macro).