为什么无法创建堆栈大小小于默认大小的线程
Why Is it not possible to create threads with stacksize less than default size?
>我写了一个小程序来创建线程并更改其默认堆栈大小,但就我而言.当我在更改属性后打印堆栈大小时,我仍然将堆栈大小显示为默认堆栈大小。
# include<iostream>
# include<pthread.h>
# include<stdio.h>
# define size 10
using namespace std;
pthread_attr_t attr;
void* sum(void * threadid)
{
static int sum =0;
sum=sum+5;
long id=(long)threadid;
size_t mystacksize;
pthread_attr_getstacksize(&attr,&mystacksize);
cout << " stack size "<< mystacksize <<endl;
}
int main()
{
int rc;
long t=0;
pthread_t threads[5]; //number of different thread to br creatd
size_t stacksize;
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr,&stacksize); //gets the default stack size
//allocated for thread
cout <<" Default stack size is : " << stacksize <<endl;
stacksize=size;
pthread_attr_setstacksize(&attr,stacksize); //sets a new stacksize for thread
for(t=0;t<5;t++)
{
rc=pthread_create(&threads[t],&attr,sum,(void*)t);
if(rc)
cout << "thread creation failed with id : " << t <<endl;
}
pthread_exit(NULL);
return 0;
}
输出是:-
Default stack size is : 8388608
stack size : 8388608
stack size : 8388608
stack size : 8388608
stack size : 8388608
stack size : 8388608
另请注意,如果我尝试将stacksize
增加到大于default size
,我就能够成功地做到这一点。
期望能够使用十个字节的堆栈大小是疯狂的。
您是否阅读了pthread_attr_setstacksize
手册页? 您是否检查了调用pthread_attr_setstacksize
的返回值?
在我的系统(GNU/Linux)上,手册页说:
pthread_attr_setstacksize() can fail with the following error:
EINVAL The stack size is less than PTHREAD_STACK_MIN (16384) bytes.
相关文章:
- 具有动态大小的特征矩阵的默认初始状态
- c++curl返回413请求实体过大,但是post大小远小于max大小
- GetWindowRect()返回的大小小于游戏的实际可见窗口的可能原因是什么
- 将地址插入到集合中,集合的大小小于预期
- 如何使用C 11样式非默认构造分配器指定初始大小
- 固定大小和零启动数组作为C 11中的默认参数
- 默认堆栈大小
- 调整小于连续数组的 C++ 矢量大小
- 创建带有括号的 std::vector 作为默认大小
- 默认矢量内存大小
- 如何在MFC中设置对话框的默认大小(以像素为单位)
- 如果缓冲区大小小于文件大小,如何合并两个文件
- 为什么无法创建堆栈大小小于默认大小的线程
- 为什么boost::asio::读取缓冲区数据大小小于读取大小
- malloc创建std::queue时的默认大小
- 如何使用Lz4库解压缩小于原始大小的文件
- std::string find是否要求pos小于字符串大小
- 如何在QT设计器或QT创建器中恢复按钮的默认大小
- std::vector/编程书籍神话的默认大小
- 如何调整Qwidget的大小,使其大于默认大小