Get and Set the stack size of thread attribute in C

CServer Side ProgrammingProgramming

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

pthread_attr_getstacksize()

Use for get threads stack size. The stacksize attribute gives the minimum stack size allocated to threads stack. In case of a successful run, then it gives 0 otherwise gives any value.

It takes two arguments −

pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize)

  • First one for pthread attribute.
  • Second one for giving the size of the thread attribute.

pthread_attr_setstacksize()

Used for set new threads stack size. The stacksize attribute gives the minimum stack size allocated to threads stack. In case of a successful run, then it gives 0 otherwise it gives any value.

It takes two arguments −

pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize)

  • First one for pthread attribute.
  • Second one for give the size of the new stack 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

Example 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\n", stacksize);
   pthread_attr_setstacksize(&a, 67626);
   pthread_attr_getstacksize(&a, &stacksize);
   printf("New stack size= %d\n", stacksize);
   return 0;
}

Output

Current stack size = 50
New stack size= 67626
raja
Published on 03-Apr-2019 15:22:24
Advertisements