Home  >  Article  >  Backend Development  >  Get and set the stack size of thread properties in C language

Get and set the stack size of thread properties in C language

WBOY
WBOYforward
2023-09-13 13:17:02855browse

Get and set the stack size of thread properties in C language

To get and set the stack size of a thread attribute in C, we use the following thread attribute:

pthread_attr_getstacksize()

is used to get the thread Stack size. The stacksize attribute gives the minimum stack size allocated to the thread stack. Returns 0 if run successfully, otherwise returns any value.

It accepts two parameters:

pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize)

  • The first parameter is the pthread attribute.
  • The second parameter is the size of the thread attribute.

pthread_attr_setstacksize()

is used to set the new thread stack size. The stacksize attribute gives the minimum stack size allocated to the thread stack. Returns 0 if run successfully, otherwise returns any value.

It accepts two parameters:

pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize)

  • The first parameter is the pthread attribute.
  • The second parameter is the new stack size in bytes.

Algorithm

Begin
   Declare stack size and declare pthread attribute a.
   Gets the current stacksize by pthread_attr_getstacksize() and print it.
   Set the new stack size by pthread_attr_setstacksize() and get the stack size pthread_attr_getstacksize() and print it.
End

Sample code

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int main() {
   size_t stacksize;
   pthread_attr_t a;
   pthread_attr_getstacksize(&a, &stacksize);
   printf("Current stack size = %d</p><p>", stacksize);
   pthread_attr_setstacksize(&a, 67626);
   pthread_attr_getstacksize(&a, &stacksize);
   printf("New stack size= %d</p><p>", stacksize);
   return 0;
}

Output

Current stack size = 50
New stack size= 67626

The above is the detailed content of Get and set the stack size of thread properties in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete