设置线程的堆栈大小

时间:2015-01-31 18:12:38

标签: c multithreading

我正在尝试一个简单的程序来设置属性 - 线程的堆栈大小。 但是使用下面的代码输出,我看到堆栈大小没有改变。

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>

#define NUMTHREADS 5

pthread_attr_t myattr;

void* mythread(void* arg){
        size_t stacksize;

        pthread_attr_getstacksize(&myattr,&stacksize);

        printf("\n tid : %u : %d stacksize : %d \n",(unsigned int)pthread_self(),(int)arg,stacksize);
        pthread_exit(NULL);
}

int main(){
        int i = 0;
        pthread_t threads[NUMTHREADS];
        pthread_attr_t myattr;
        size_t stacksize;

        /*Initialse and set thread detach attribute */
        pthread_attr_init(&myattr);
//      pthread_attr_setdetachstate(&myattr,PTHREAD_CREATE_JOINABLE);
        pthread_attr_getstacksize(&myattr,&stacksize);
        printf("\n Initial Stack size : %d \n",stacksize);

        stacksize = stacksize + 100;

        pthread_attr_setstacksize(&myattr,stacksize);

        //pthread_attr_destroy(&myattr);

        for(i = 0; i < NUMTHREADS; i++){
                printf("\n Creating Threads! \n");
                pthread_create(&threads[i],&myattr,&mythread,(void*)i);
        }

#if 1
        for(i = 0; i < NUMTHREADS; i++){
                printf("\n Joining Threads! \n");
                pthread_join(threads[i],NULL);
        }
#endif

        pthread_attr_getstacksize(&myattr,&stacksize);
        printf("\n Initial Stack size : %d \n",stacksize);

        printf("\n Main tid : %u \n",(unsigned int)pthread_self());
        pthread_exit(NULL);
}

输出:

angus @ ubuntu:〜/ angus / thread $ ./a.out

初始堆栈大小:8388608

创建线程!

创建线程!

创建线程!

创建线程!

创建线程!

加入主题!

tid:2443623808:4 stacksize:8388608

tid:2443726208:3 stacksize:8388608

tid:2443828608:2 stacksize:8388608

tid:2443931008:1 stacksize:8388608

tid:2444033408:0 stacksize:8388608

加入主题!

加入主题!

加入主题!

加入主题!

主要时间:2444044032

编辑: 编辑后的O / P根据“R”答案评论pthread_attr_destroy()

angus @ ubuntu:〜/ angus / thread $ ./a.out

初始堆栈大小:8388608

创建线程!

创建线程!

创建线程!

创建线程!

创建线程!

加入主题!

tid:855779136:4 stacksize:8388608

tid:864171840:3 stacksize:8388608

tid:872564544:2 stacksize:8388608

tid:880957248:1 stacksize:8388608

tid:889349952:0 stacksize:8388608

加入主题!

加入主题!

加入主题!

加入主题!

初始堆栈大小:8388708

主要筹码:897656576

2 个答案:

答案 0 :(得分:1)

您在销毁后使用属性对象,因此行为未定义。删除此行或在创建线程后移动它:

    pthread_attr_destroy(&myattr);

答案 1 :(得分:0)

有一种方法可以测量堆栈大小,但是容易出错。

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include <unistd.h>

#define NUMTHREADS 2

pthread_attr_t myattr;


void* mythread(void* arg){
        char stack_start = (char) (unsigned int)pthread_self();  //make sure compiler does not optimize us out 
        // int huge_stack_array[4096] = {1,2,3};
        printf("\nhello, im the beginning of tid:%u stack > %p <",(unsigned int)pthread_self(), &stack_start);
        pthread_exit(NULL);
}

int main(){
        int i = 0;
        pthread_t threads[NUMTHREADS];
        pthread_attr_t myattr;
        size_t stacksize;
        pthread_attr_init(&myattr);
        pthread_attr_getstacksize(&myattr,&stacksize);
        printf("\n Initial Stack size : %d \n",stacksize);
        stacksize = 1024*16;
        if (pthread_attr_setstacksize(&myattr,stacksize)) goto failed;
        printf("\n Stack size now : %d \n",stacksize);
        for(i = 0; i < NUMTHREADS; i++){
                pthread_create(&threads[i],&myattr,&mythread,(void*)i);
        }
#if 1
        for(i = 0; i < NUMTHREADS; i++){
                pthread_join(threads[i],NULL);
        }
#endif

        stacksize = 1024*28;
        if (pthread_attr_setstacksize(&myattr,stacksize)) goto failed;
        printf("\n Stack size now : %d \n",stacksize);
        for(i = 0; i < NUMTHREADS; i++){
                pthread_create(&threads[i],&myattr,&mythread,(void*)i);
        }
#if 1
        for(i = 0; i < NUMTHREADS; i++){
                pthread_join(threads[i],NULL);
        }
#endif
        pthread_attr_destroy(&myattr);
        printf("\n Main tid : %u \n",(unsigned int)pthread_self());
        pthread_exit(NULL);

        failed: 
                printf("Can't set stacksize - is it multiplication of PAGE_SIZE, and >16384?\n");
}

输出:

 $ gcc -pthread  test.c -o test && ./test

 Initial Stack size : 8388608 

 Stack size now : 16384 

hello, im the beginning of tid:3078323008 stack > 0xb77b735f <
hello, im the beginning of tid:3078306624 stack > 0xb77b335f <
 Stack size now : 28672 

hello, im the beginning of tid:3078290240 stack > 0xb77af35f <
hello, im the beginning of tid:3078261568 stack > 0xb77a835f <
 Main tid : 3076368064 

现在,calc开始工作了:

$ calc
; 0xb77b735f-0xb77b335f
    16384
; 0xb77af35f-0xb77a835f
    28672

哦,是的,如果你在第一个线程堆栈上创建了int huge_array [4096],那就是segfaults,而第二个就是按预期工作。