要在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