在 C 中获取并设置线程属性的堆栈大小


要在 C 中获取并设置线程属性的堆栈大小,请使用以下线程属性

pthread_attr_getstacksize()

用于获取线程堆栈大小。stacksize 属性给出了分配给线程堆栈的最小堆栈大小。如果运行成功,则返回 0,否则返回任何值。

它需要两个参数 -

pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize)

  • 第一个是 pthread 属性。
  • 第二个是用于指定线程属性大小。

pthread_attr_setstacksize()

用于设置新线程堆栈大小。stacksize 属性给出了分配给线程堆栈的最小堆栈大小。如果运行成功,则返回 0,否则返回任何值。

它需要两个参数 -

pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize)

  • 第一个是 pthread 属性。
  • 第二个是用于指定新堆栈的大小(以字节为单位)。

算法

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

示例代码

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

输出

Current stack size = 50
New stack size= 67626

更新日期:2019-07-30

1k+ 次浏览

开始了您的职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.