Thread attribute: pthread_attr_t

Source: Internet
Author: User

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?
  1. # Include <stdio. h>
  2. # Include <pthread. h>
  3. # Include <string. h>
  4. Void* Thr_fn ()
  5. {
  6. Printf ("thread run \ n ");
  7. Pthread_exit ((Void*) 0 );
  8. }
  9. IntMain ()
  10. {
  11. Pthread_t tid;
  12. Pthread_attr_t ATTR;
  13. IntRET;
  14. Ret = pthread_attr_init (& ATTR );
  15. If(Ret! = 0)
  16. Printf ("init ATTR error: % s \ n", strerror (RET ));
  17. Ret = pthread_attr_setdetachstate (& ATTR, pthread_create_detached );
  18. If(Ret = 0)
  19. {
  20. Ret = pthread_create (& tid, & ATTR, thr_fn, null );
  21. If(Ret! = 0)
  22. Printf ("create thread error: % s \ n", strerror (RET ));
  23. }
  24. Pthread_attr_destroy (& ATTR );
  25. Sleep (1 );
  26. Return0;
  27. }

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.