Hardware Reference
In-Depth Information
At this point, the main thread and the function
my_thread()
execute in parallel.
Since there is only one CPU on the Raspberry Pi, only one executes at any instant of time.
But they both execute concurrently, trading blocks of execution time in a preemptive
manner. Each, of course, runs using its own stack.
Thread
my_thread()
terminates gracefully, by returning.
pthread_attr_t
There are several thread attributes that can be fetched and set. You'll look only at perhaps
the most important attribute (stack size) to keep this crash course brief. For the full list of
attributes and functions, you can view the man pages for it:
$ man pthread_attr_init
To initialize a new attribute, or to release a previously initialized pthread attribute,
use this pair of routines:
int pthread_attr_init(pthread_attr_t
attr);
int pthread_attr_destroy(pthread_attr_t
∗
attr);
∗
attr
: Address of the
pthread_attr_t
variable to initialize/
destroy
returns
: Zero upon success, or an error code when it fails
(not in
errno
)
Error
Description
ENOMEM
Insufficient resources (memory)
The Linux implementation of
pthread_attr_init(3)
may never return the
ENOMEM
error, but other Unix platforms might.
The following is a simple example of creating and destroying an attribute object:
pthread_attr_t attr;
pthread_attr_init(&attr); // Initialize attr
...
pthread_attr_destroy(&attr); // Destroy attr
Perhaps one of the most important attributes of a thread is the stack size attribute:
int pthread_attr_setstacksize(
pthread_attr_t *attr,
size_t stacksize
);