Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Thread attributes in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of thread attributes in Linux", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of thread attributes in Linux".

When we call the pthread_create function to create a thread, the second parameter (that is, the thread property) is set to NULL, even if the default property is used. In general, using default properties can already solve most of the problems in our development process.

However, sometimes we have some special requirements for threads in the project, such as modifying the size of the thread stack and directly calling the thread's library function can not meet the requirements. In this case, we can directly set the thread properties.

Type pthread_attr_t is a structure that mainly includes the following attributes: scope (scope), stack size (stack size), stack address (stack address), priority (priority), detached state (detached state), scheduling policy and parameters (scheduling policy and parameters).

The default properties of a thread are unbound, non-detached, default stack, and priority at the same level as the parent process. The structure is defined as follows:

Main structural members:

Thread separation status: etachstate

Thread stack size (default average distribution): stacksize

Thread stack alert buffer size (at the end of the stack): guardsize

The property value of the thread cannot be set directly and must be operated using the relevant function. Property is initialized with pthread_attr_init, which must be called before the pthread_create function. After you finish using it, you need to call the pthread_attr_destroy function to release the resources.

# # initialization of thread attributes

Function prototype:

Int pthread_attr_init (pthread_attr_t * attr)

Return value:

Success: 0; failure: error number.

Function function: initializes thread properties

Note: you should initialize the thread properties before calling pthread_create to create the thread.

# # destroy thread attributes

Function prototype:

Int pthread_attr_destroy (pthread_attr_t * attr)

Return value:

Success: 0; failure: error number

Function function: destroys the resources occupied by thread attributes

# # Separation status of threads

The detached state of a thread determines how a thread reclaims resources when it finally terminates.

Non-detached state: the default property of a thread is a non-detached state, in which case, after the thread ends, only when other threads call the pthread_join () function to reclaim it, the created thread is terminated and the system resources occupied by itself can be released.

Detached state: if the thread is set to the detached state, it will actively disconnect from the master thread. When its own running ends, the thread terminates, releasing system resources immediately.

The function that sets the thread separation state:

Set thread properties

Int pthread_attr_setdetachstate (pthread_attr_t * attr, int detachstate)

Get program properties

Int pthread_attr_getdetachstate (pthread_attr_t attr, int detachstate)

Parameter: attr: a pointer to a thread property

Detachstate: thread separation state

PTHREAD_CREATE_DETACHED (detach thread)

PTHREAD _ CREATE_JOINABLE (non-detached thread)

# # Stack address of thread

POSIX.1 defines two constants, _ POSIX_THREAD_ATTR_STACKADDR and _ POSIX_THREAD_ATTR_STACKSIZE, to check whether the system supports stack attributes. You can also pass _ SC_THREAD_ATTR_STACKADDR or _ SC_THREAD_ATTR_STACKSIZE to the sysconf function for detection.

When the process stack address space is insufficient, specify that the newly created thread uses the space allocated by the malloc as its own stack space. The stack address of the thread is set and obtained through the pthread_attr_setstack and pthread_attr_getstack functions, respectively.

Set the stack address of the thread:

Int pthread_attr_setstack (pthread_attr_t attr, void stackaddr, size_t stacksize)

Success: 0; failure: error number

Get the stack address of the thread:

Int pthread_attr_getstack (pthread_attr_t attr, void * * stackaddr, size_t stacksize)

Success: 0; failure: error number

Parameter: attr: a pointer to a thread property

Stackaddr: returns the acquired stack address

Stacksize: returns the stack size obtained

# # Stack size of Thread

When there are many threads in the system, you may need to reduce the default size of each thread stack to prevent the process from running out of address space. When the function called by the thread allocates a large local variable or the function call is deep, you may need to increase the default size of the thread stack.

The functions pthread_attr_getstacksize and pthread_attr_setstacksize can set or get the stack size of a thread.

Set the thread stack size:

Int pthread_attr_setstacksize (pthread_attr_t * attr, size_t stacksize)

Success: 0

Failure: error number

Get the thread stack size:

Int pthread_attr_getstacksize (pthread_attr_t attr, size_t stacksize)

Success: 0

Failure: error number

Parameter: attr: a pointer to a thread property

Stacksize: returns the stack size of the thread

# include # define SIZE 0x10000 void * th_fun (void * arg) {while (1) sleep (1);} int main (void) {pthread_t tid; int err, detachstate, I = 1; pthread_attr_t attr; size_t stacksize; / / typedef size_t unsigned int void * stackaddr; pthread_attr_init (& attr) Pthread_attr_getstack (& attr, & stackaddr, & stacksize); pthread_attr_getdetachstate (& attr, & detachstate); if (detachstate = = PTHREAD_CREATE_DETACHED) / / defaults to separated printf ("thread detached\ n"); else if (detachstate = = PTHREAD_CREATE_JOINABLE) / / defaults to non-detached printf ("thread join\ n"); else printf ("thread un known\ n") / * set thread separation properties * / pthread_attr_setdetachstate (& attr, PTHREAD_CREATE_DETACHED); while (1) {/ * request memory on the heap, specify the starting address and size of the thread stack * / stackaddr = malloc (SIZE); if (stackaddr = = NULL) {perror ("malloc"); exit (1) } stacksize = SIZE; pthread_attr_setstack (& attr, stackaddr, stacksize); / / modify thread stack space size err = pthread_create (& tid, & attr, th_fun, NULL); if (err! = 0) {printf ("% s\ n", strerror (err)); exit (1) } printf ("% d\ n", iTunes +);} pthread_attr_destroy (& attr); return 0;} above is all the content of the article "sample Analysis of Thread attributes in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report