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;