Use of thread stack size pthread_attr_setstacksize

Source: Internet
Author: User

Author: Zhang jifei

When pthread_create creates a thread, if the stack size is not specified, the system allocates the default value. The default value is as follows:

# Ulimit-S
8192
#

The above expression is 8 m;Unit: KB.

You can also use # ulimit-A, where the stack size item also indicates the stack size. Ulimit-s value is used to reset the stack size.

GenerallyThe default stack size is 8388608, and the minimum stack size is 16384. The Unit is byte.

The minimum value of the stack is definedPthread_stack_min, including # include <limits. h>, which can be viewed by printing the value. For default values, you can use pthread_attr_getstacksize (& ATTR, & stack_size); To print stack_size.

Especially in embedded systems, the memory is not very large. If the default value is used, it may cause problems. If the memory is insufficient, pthread_create will return 12, which is defined as follows:

# Define Eagain 11

# Define enomem 12/* out of memory */

We learned about the stack size above. Next we will learn how to use pthread_attr_setstacksize to reset the stack size. Let's take a look at its prototype:

# Include <pthread. h>
Int pthread_attr_setstacksize (pthread_attr_t *ATTR, Size_tStacksize);

ATTR is the thread attribute variable, while stacksize is the set stack size. The return values 0 and-1 indicate success and failure respectively.

Here is how to use

Pthread_t thread_id;

Int ret, stacksize = 20480 ;/*The thread stack is set to 20 KB, And the stacksize is measured in bytes..*/

Pthread_attr_t ATTR;
Ret = pthread_attr_init (& ATTR);/* initialize thread attributes */

If (Ret! = 0)

Return-1;
Ret = pthread_attr_setstacksize (& ATTR, stacksize );

If (Ret! = 0)

Return-1;
Ret = pthread_create (& thread_id, & ATTR, & func, null );

If (Ret! = 0)

Return-1;
Ret = pthread_attr_destroy (& ATTR);/* destroy thread attributes instead of using them */

If (Ret! = 0)

Return-1;

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.