Linux C Programming: Thread Properties

Source: Internet
Author: User

The Pthread_create function is described earlier, and in the case of the time, the parameters passed in are null pointers, not pointers to pthread_attr_t structures. You can use the pthread_attr_t structure to modify thread default properties and associate these attributes with the created thread. You can use the Pthread_attr_init function to initialize the pthread_attr_t structure. After calling Pthread_attr_init, the pthread_attr_t structure contains the default values for all the properties that the operating system implements to support threads. If you want to modify the values of individual properties, you need to call other functions.

#include <pthread.h>

int Pthread_attr_init (pthread_attr_t *attr);

int Pthread_attr_destroy (pthtread_attr_t *attr);

The return value of two functions is: returns 0 if successful, otherwise returns the error number

If you want to remove the initialization of the pthread_attr_t structure, you can call the Pthread_attr_destroy function. If Pthread_attr_init is implemented with a dynamic memory space assigned to the Property object, Pthread_attr_destroy will free that memory space. In addition, Pthread_attr_destroy also initializes the Property object with an invalid value, so if the Property object is misused, it will cause the Pthread_create function to return an error.

The pthread_attr_t structure is transparent to the application, which means that the application does not need to know any details about the internal structure of the Property object, thereby enhancing the portability of the application. Posix.1 follows this model and defines a separate function for querying and setting each property

name

Description

Detachstate

Detach State Properties for threads

Guardsize

The size of the alert buffer at the end of the thread stack (bytes)

Stackaddr

Minimum address for line stacks

StackSize

Size of line stacks (bytes)

If you are not interested in the termination state of one of the existing threads, you can use the Pthread_detach function to reclaim the resources that the operating system consumes when the thread exits.

If you know that you do not need to know the terminating state of a thread when creating a thread, you can modify the Detachstate thread properties in the pthread_attr_t structure to let the thread start in a detached state. You can use the Pthread_attr_setdetachstate function to set the thread property detachstate to one of the following two valid values: Set to pthread_create_detached, start thread in detach state or set to Pthread_create_joinable, the thread is started normally, and the application can get the terminating state of the thread.

#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);
The return value of both is: returns 0 if successful, otherwise returns the error number
You can call the Pthread_attr_getdetachstate function to get the current Detachstate thread property, and the integer pointed to by the second argument is used to save the obtained Detachstate property value: Pthread_create_ DETACHED, or pthread_create_joinable).
Instance
Programs: Threads created in detach state
Copy Code
#include "apue.h"
#include <pthread.h>
Int
Makethread (void * (*FN) (void *), void *arg)
{
int err;
pthread_t Tid;
pthread_attr_t attr;
Err = Pthread_attr_init (&attr);
if (err! = 0)
return (ERR);
Err = Pthread_attr_setdetachstate (&attr, pthread_create_detached);
if (err = = 0)
Err = Pthread_create (&tid, &attr, FN, ARG);
Pthread_attr_destroy (&ATTR);
return (ERR);
}
This ignores the return value of the Pthread_attr_destroy function call. In this case, Pthread_attr_destroy generally does not fail due to reasonable initialization of thread properties. But if Pthread_attr_destroy does fail, it becomes difficult to clean up: the thread you just created must be destroyed, and the thread may already be running, and the Pthread_attr_destroy function might be executed asynchronously. The worst-case scenario for ignoring Pthread_attr_destroy error returns is that if Pthread_attr_init allocates memory space, the memory space is compromised. On the other hand, if pthread_attr_init successfully initializes the thread properties, but Pthread_attr_destroy fails to clean up, there is no remediation policy because the thread property structure is transparent to the application. The only interface that can clean up the thread property structure is Pthread_attr_destroy, but it fails.
Threading stack attributes are not necessarily supported for operating systems that follow the POSIX standard, but support for thread stack properties is required for systems that follow XSI. You can use the _POSIX_THREAD_ATTR_STACKADDR and _posix_attr_stacksize symbols in the compile phase to check whether the system supports thread stack properties, and if the system defines these symbols, it supports the corresponding thread stack properties. You can also check the system's support for thread stack properties by passing the _SC_THREAD_ATTR_STACKADDR and _sc_thread_attr_stacksize parameters to the sysconf function at run time.
Posix.1 defines some of the operating interfaces for threading stack properties. The query and modification of thread stack properties is generally done through newer functions pthread_attr_getstack and pthread_attr_setstack.
#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);
The return value of both is: returns 0 if successful, otherwise returns the error number
Using the Pthread_attr_setstacksize function is useful when you want to change the default size of the stack, but do not want to handle the allocation of line stacks yourself.
The Thread property Guardsize controls the size of the extended memory that is used to avoid stack overflow after the end of the thread stack. This property is set to pagesize bytes by default. You can set the Guardsize thread property to 0, which does not allow this characteristic behavior of the attribute to occur: In this case the alert buffer is not provided. Similarly, if the thread attribute stackaddr is modified, the system assumes that we will manage the stack ourselves and invalidate the alert stack buffer mechanism, which is equivalent to setting the Guardsize property to 0.
Copy Code
#include <pthread.h>
size_t *restrict guardsize);
int Pthread_attr_setguardsize (pthread_attr_t *attr, size_t guardsize);
The return value of both is: returns 0 if successful, otherwise returns the error number
If the Guardsize thread property is modified, the operating system may take it as an integer multiple of the page size. If a thread's stack pointer overflows into the alert area, the application may receive an error message via the signal.

Linux C Programming: Thread Properties

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.