线程创建时指定线程栈的大小

    xiaoxiao2021-03-25  35

    int ithread_start(void *(run)(void *), void *arg) {

    pthread_t threadId;

    pthread_attr_t threadAttr;

    memset(&threadAttr,0,sizeof(pthread_attr_t));

    pthread_attr_init(&threadAttr);

        int status = 0;

        size_t size = 0;

        //printf("default size:%d\n", size);

        status = pthread_attr_getstacksize(&threadAttr, &size);

        if(0 != status)

    {

      printf("pthread_attr_getstacksize err [%d]\n",status);

    }

    printf("current thread stack size:%d\n", size);

    size = 1024*1024;

    status = pthread_attr_setstacksize(&threadAttr, size);

    if(0 != status)

    {

    printf("pthread_attr_getstacksize err [%d]\n",status);

    return -1;

    }

    printf("set thread stack size:%d\n", size);

    pthread_attr_setdetachstate(&threadAttr,PTHREAD_CREATE_DETACHED);

    pthread_create(&threadId, &threadAttr, run, arg);

    pthread_attr_destroy(&threadAttr);

    printf("%d\n", (int)threadId);

    return threadId;

    }

    此处代码修改线程的栈大小为1M

    扩展:

    1、  获取Linux默认线程栈大小

    ulimite -s

    2、  修改Linux默认线程栈大小

    ulimite -s value

    转载请注明原文地址: https://ju.6miu.com/read-50236.html

    最新回复(0)