2012-05-17 3 views
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을 늘리면 내가 그렇게 성공적으로 수 있어요 있습니다.

+0

언어 태그를 추가하십시오. C/C++라고 가정합니다. – Gray

+0

왜 작은 스택 크기를 원하나요? –

+0

@HotLicks 나는 스레드 개념의 초보자이며, 몇 가지 걸레질을했다. – Invictus

답변

4

10 바이트 크기의 스택을 사용할 수있을 것으로 예상됩니다.

pthread_attr_setstacksize 매뉴얼 페이지를 읽었습니까? 전화 pthread_attr_setstacksize의 반환 가치를 확인 했습니까? 내 시스템에서

은 (GNU/리눅스) man 페이지는 말한다 :

pthread_attr_setstacksize() can fail with the following error: 

    EINVAL The stack size is less than PTHREAD_STACK_MIN (16384) bytes. 
관련 문제