9.3.2 Use of void* type
ANSI C requires that you be allowed to convert any pointer type to void* and back, with the result being identical to the original value. However, ANSI C does not require that all pointer types have the same binary representation. Thus, a long* that you convert to void* in order to pass into a thread's start routine must always be used as a long*, not as, for example, a char*. In addition, the result of converting between pointer and integer types is 'implementation defined.' Most systems supporting UNIX will allow you to cast an integer value to void* and back, and to mix pointer types — but be aware that the code may not work on all systems.
Some other standards, notably the POSIX. lb realtime standard, have solved the same problem (the need for an argument or structure member that can take any type value) in different ways. The sigevent structure in POSIX.1b, for example, includes a member that contains a value to be passed into a signal-catching function, called sigev_value. Instead of defining sigev_value as a void*, however, and relying on the programmer to provide proper type casting, the sigev_ value member is a union sigval, containing overlayed int and void* members. This mechanism avoids the problem of converting between integer and pointer types, eliminating one of the conflicts with ANSI C guarantees.
9.3.3 Threads
Threads provide
pthread_attr_destroy
int pthread_attr_destroy (
pthread_attr_t *attr);
Destroy a thread attributes object. The object can no longer be used.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL]attr is invalid.
Hint: Does not affect threads created using attr.
pthread_attr_getdetachstate
int pthread_attr_getdetachstate (
const pthread_attr_t *attr, int *detachstate);
Determine whether threads created with attr will run detached
detachstate
PTHREAD CREATE JOINABLE
PTHREAD CREATE DETACHED
Thread ID is valid, must be joined.
Thread ID is invalid, cannot be joined, canceled, or modified.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL] attr is invalid.
Hint: You can't join or cancel detached threads.
pthread_attr_getstackaddr..................................................... [_POSIX_THREAD_ATTR_STACKADDR]
int pthread_attr_getstackaddr (
const pthread_attr_t *attr,
void **stackaddr);
Determine the address of the stack on which threads created with attr will run.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL]attrisinvalid.
[ENOSYS] stacksize not supported. Hint: Create only one thread for each stack address!
pthread_attr_getstacksize....................................................... [_POSIX_THREAD_ATTR_STACKSIZE]
int pthread_attr_getstacksize (
const pthread_attr_t *attr, size_t *stacksize);
Determine the size of the stack on which threads created with attr will run.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL]attrinvalid.
[ENOSYS] stacksize not supported. Hint: Use on newly created attributes object to find the default stack size.
pthread_attr_init
int pthread_attr_init (
pthread_attr_t *attr);
Initialize a thread attributes object with default attributes.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [ENOMEM] insufficient memory for attr.
Hint: Use to define thread types.
| pthread_aftr_setdetachstate
int pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate);
Specify whether threads created with attr will run
detachstate
PTHREAD CREATE JOINABLE
PTHREAD CREATE DETACHED
Thread ID is valid, must be joined.
Thread ID is invalid, cannot be joined, canceled, or modified.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL]attrinvalid.
[EINVAL] detachstate invalid. Hint: You can't join or cancel detached threads.
pthread_attr_setstackaddr..................................................... [_POSIX_THREAD_ATTR_STACKADDR]
int pthread_attr_setstackaddr ( pthread_attr_t *attr, void *stackaddr);
Threads created with attr will run on the stack starting at stackaddr. Must be at least PTHREAD_STACK_MIN bytes.
References: 2, 5.2.3
Headers: <pthread.h>
Errors: [EINVAL]attrinvalid.
[ENOSYS] stackaddr not supported. Hint: Create only one thread for each stack address, and be careful of
stack alignment.