卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20085本站已运行345

获取和设置C语言中线程属性的堆栈大小

获取和设置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<p>", stacksize);
   pthread_attr_setstacksize(&a, 67626);
   pthread_attr_getstacksize(&a, &stacksize);
   printf("New stack size= %d</p><p>", stacksize);
   return 0;
}</p>

输出

Current stack size = 50
New stack size= 67626
卓越飞翔博客
上一篇: 如何使用PHP队列发送定时短信?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏