Home > Backend Development > C++ > 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
Release: 2023-09-13 13:17:02
forward
914 people have browsed it

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
Copy after login

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;
}
Copy after login

Output

Current stack size = 50
New stack size= 67626
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template