1
스레드를 만들고 기본 스택 크기를 변경하는 작은 프로그램을 작성했지만 제 경우에는 그렇습니다. 특성을 변경 한 후에 stacksize를 인쇄 할 때 stacksize가 여전히 기본 스택 크기로 표시됩니다.왜 스택 크기가 기본 크기보다 작은 스레드를 만들 수 없습니까?
# 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
을 늘리면 내가 그렇게 성공적으로 수 있어요 있습니다.
언어 태그를 추가하십시오. C/C++라고 가정합니다. – Gray
왜 작은 스택 크기를 원하나요? –
@HotLicks 나는 스레드 개념의 초보자이며, 몇 가지 걸레질을했다. – Invictus