pthread_attr_t Thread Properties (ii)

Source: Internet
Author: User

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
  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. int main ()
  10. {
  11. pthread_t Tid;
  12. pthread_attr_t attr;
  13. int ret;
  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. return 0;
  27. }

Run:
[Email protected]:~/12#./a.out
Thread Run

pthread_attr_t Thread Properties (b)

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.