POSIX thread的 stack大小设置(之二)
来源:互联网 发布:pdfconverter软件下载 编辑:程序博客网 时间:2024/06/06 19:30
以前曾经写过一篇关于thread stack size设置的文章,现在感觉有些地方没有说清楚,所以现在再讲一讲。
1)曾经有个问题,就是设置下去 stack size之后,再去读的时候返回的还是之前的数值。
原因没有正确使用POSIX threads 的接口。
下面的代码是可以正确的读出数值的:
#include <pthread.h>#include <stdio.h>void * thread_start(void *arg){pthread_attr_t attr;int size = 0;//int *stackaddr = NULL;//pthread_t * tid = (pthread_t *)arg;pthread_getattr_np(pthread_self(), &attr);pthread_attr_getstacksize(&attr, &size);printf("stack size: %d KB\n", size/1024);size *= 2;if(pthread_attr_setstacksize(&attr, size) != 0){printf("failed to set stack size\n");}pthread_attr_getstacksize(&attr, &size);printf("size = %dKB\n", size/1024);//char arr[1024 * 257];}int main(void){pthread_t tid;pthread_attr_t attr;void *status = NULL;pthread_attr_init(&attr);//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);pthread_attr_setstacksize(&attr, 256 * 1024);pthread_create(&tid, &attr, &thread_start, NULL);//pthread_exit(NULL);pthread_attr_destroy(&attr);pthread_join(tid, &status);return 0;}
输出为:
#./code/threadstackstack size: 256 KBsize = 512KB
2)另外需要注意的一点是,线程的stack 大小只能在主线程里设置,而且一旦设置后,就固定下来而无法改变。
上面的这个代码在线程函数里去尝试修改线程的 stack 大小,虽然表面看成功了,但实际上线程的 stack 大小还是256KB!
这一点可以通过用 GDB 调试,观察程序的 内存映射里的 stack 区域的大小确认。
这在 pthread的 man 里也有说明:
A thread's stack size is fixed at the time of thread creation. Only the main thread can dynamically grow its stack.
0 0
- POSIX thread的 stack大小设置(之二)
- POSIX thread的 stack大小设置
- POSIX thread(pthread) (二)
- linux网络编程之posix 线程(二):线程的属性和 线程特定数据 Thread-specific Data
- 根据屏幕大小设置显示图片的大小(二)
- linux的线程(posix thread)
- POSIX thread
- posix thread
- POSIX Thread之one-time initializition
- 一起来学POSIX thread 之 基本概念
- 一起来学POSIX thread 之 线程状态
- 一起来学POSIX thread 之 线程的创建和基本操作接口
- POSIX多线程程序设计学习篇之二(互斥量)
- POSIX设置锁的属性
- apue thread posix thread posix线程相关
- THREAD STACK
- Thread系列之ManualResetEvent(二)
- 一起来学POSIX thread 之 线程利弊与选择
- Fragment的生命周期及相关介绍
- Jquery实现回车键Enter切换焦点
- Struts2整合Spring有关问题
- There is no Action mapped for namespace / and action name UserAction
- AppCan里面的Ajax通信实例
- POSIX thread的 stack大小设置(之二)
- Python自动单元测试框架学习
- iOS开发之数组去重与排序
- 通过Annotation来实现AOP
- mysqldump死住(实际是导致mysqld crash)
- poj 3261 后缀数组 找重复出现k次的子串(子串可以重叠)
- 认识 C++ 中的explicit 关键字
- 诺基亚5800入手十天谈感受
- 算法面试(数组)