I. functions:
1. Initialization and destruction of thread attributes:
# 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 the thread Property -- detackstate (separation 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 separation status startup thread
Pthread_create_joinable normal startup thread
3. Set the thread attributes-stackaddr (the lowest address of the thread stack) and stacksize (the size of the thread stack ):
# 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 the thread attribute -- stacksize (the size of the thread stack ):
# 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 the thread Attribute-guardsize (the 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
II. Key points:
Iii. Example:
Creates a thread in a separate state.
[CPP]
View plaincopyprint?
- # Include <stdio. h>
- # Include <pthread. h>
- # Include <string. h>
- Void* Thr_fn ()
- {
- Printf ("thread run \ n ");
- Pthread_exit ((Void*) 0 );
- }
- IntMain ()
- {
- Pthread_t tid;
- Pthread_attr_t ATTR;
- IntRET;
- 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 );
- Return0;
- }
# Include <stdio. h> <br/> # include <pthread. h> <br/> # include <string. h> </P> <p> void * thr_fn () <br/> {<br/> printf ("thread run \ n "); <br/> pthread_exit (void *) 0); <br/>}</P> <p> int main () <br/>{< br/> pthread_t tid; <br/> pthread_attr_t ATTR; <br/> int ret; </P> <p> ret = pthread_attr_init (& ATTR); <br/> If (Ret! = 0) <br/> printf ("init ATTR error: % s \ n", strerror (RET); <br/> ret = pthread_attr_setdetachstate (& ATTR, pthread_create_detached ); <br/> If (ret = 0) <br/> {<br/> ret = pthread_create (& tid, & ATTR, thr_fn, null ); <br/> If (Ret! = 0) <br/> printf ("create thread error: % s \ n", strerror (RET )); <br/>}< br/> pthread_attr_destroy (& ATTR); <br/> sleep (1); <br/> return 0; <br/>}Run:
Root @ ubuntu1 :~ /12 #./A. Out
Thread run
From http://blog.csdn.net/wyjlovec/article/details/7347128