A. Function:
1. Initialization and destruction of thread properties:
#include <pthread.h>
int Pthread_attr_init (pthread_attr_t *attr);
int Pthread_attr_destroy (pthread_attr_t *attr);
Both return:0 if OK, error number on failure
2. Set thread Properties--detackstate (Detach State):
#include <pthread.h>
int pthread_attr_getdetachstate (const pthread_attr_t *restrict attr, int *detachstate);
int Pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);
Both return:0 if OK, error number on failure
Detachstate has two options: pthread_create_detached detach state to start thread
pthread_create_joinable normal state start thread
3. Set thread properties--stackaddr (lowest address of line stacks), stacksize (size of line stacks):
#include <pthread.h>
int Pthread_attr_getstack (const pthread_attr_t *restrict attr,
void **restrict Stackaddr,
size_t *restrict stacksize);
int Pthread_attr_setstack (const pthread_attr_t *attr,
void *stackaddr,
size_t *stacksize);
Both return:0 if OK, error number on failure
4. Set thread properties--stacksize (size of line stacks):
#include <pthread.h>
int pthread_attr_getstacksize (const pthread_attr_t *restrict attr,
size_t *restrict stacksize);
int Pthread_attr_setstacksize (pthread_attr_t *attr,
size_t stacksize);
Both return:0 if OK, error number on failure
5. Set thread properties--guardsize (alert buffer size at the end of the thread stack)
#include <pthread.h>
int pthread_attr_getguardsize (const pthread_attr_t *restrict attr,
size_t *restrict guardsize);
int Pthread_attr_setguardsize (pthread_attr_t *attr,
size_t guardsize);
Both return:0 if OK, error number on failure
two. Focus:
Three. Example:
Create a thread in detach state
[CPP]View Plaincopy
- #include <stdio.h>
- #include <pthread.h>
- #include <string.h>
- void * THR_FN ()
- {
- printf ("Thread run\n");
- Pthread_exit ((void *) 0);
- }
- int main ()
- {
- pthread_t Tid;
- pthread_attr_t attr;
- int ret;
- ret = Pthread_attr_init (&attr);
- if (ret!=0)
- printf ("Init attr error:%s\n", Strerror (ret));
- ret = Pthread_attr_setdetachstate (&attr,pthread_create_detached);
- if (ret==0)
- {
- ret = Pthread_create (&tid,&attr,thr_fn,null);
- if (ret!=0)
- printf ("Create thread error:%s\n", strerror (ret));
- }
- Pthread_attr_destroy (&ATTR);
- Sleep (1);
- return 0;
- }
Run:
[Email protected]:~/12#./a.out
Thread Run
pthread_attr_t Thread Properties (b)