免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2251 | 回复: 4
打印 上一主题 下一主题

[Linux] 设置线程栈大小,不起作用,请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-18 17:28 |只看该作者 |倒序浏览
本帖最后由 zhongfangqing 于 2013-09-18 17:31 编辑

//各位牛哥、牛姐、牛弟、牛妹:
//下面这坨代码,目的是想设置新线程的栈大小,但是不起作用。在下环境Red hat entriprise 6.2, 32位
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <limits.h>

typedef int BOOL;
#define TRUE 1
#define FALSE 0


void *ThOfCreateThread(void *pParam)
{
        pthread_attr_t attr;
        int nRet = pthread_attr_init(&attr);
        if(0 != nRet)
        {
                printf("!!ThOfCreateThread,1,pthread_attr_init fail,err=%d,%s\n", errno, strerror(errno));
                return NULL;
        }

        size_t tThStackSize = 0;
        nRet = ::pthread_attr_getstacksize(&attr, &tThStackSize);

        printf("ThOfCreateThread,4,pthread_attr_getstacksize,tThStackSize=%d\n", tThStackSize);

        nRet = pthread_attr_destroy(&attr);
        if(0 != nRet)
        {
                printf("!!ThOfCreateThread,7,pthread_attr_destroy fail,err=%d,%s\n", errno, strerror(errno));
        }

        return NULL;
}

void TestCreateThread()
{
        pthread_attr_t attr;
        struct sched_param param;

        pthread_t m_thread = 0;
        size_t m_dwStackSize = 1024 * 1024 * 2;
        int nRet = pthread_attr_init(&attr);
        if(0 != nRet)
        {
                printf("!!TestCreateThread,1,pthread_attr_init fail,err=%d,%s\n", errno, strerror(errno));
                return ;
        }
#if 0
        pthread_attr_setschedpolicy(&attr, nPolicy);
        pthread_attr_getschedparam(&attr, &param);
        param.__sched_priority = nPriority;
        pthread_attr_setschedparam(&attr, &param);
#endif

        //add by zfq,2013.09.17,begin
        if(0 != m_dwStackSize && 10240 < m_dwStackSize)
        {
                int nRet = 0;
                size_t tThStackSize = 0;

                nRet = ::pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);        //设置为非分离线程
                if( 0 != nRet)
                {
                        printf("!!TestCreateThread,2,setdetachstate fail,err=%d,%s\n", errno, strerror(errno));
                }

                nRet = ::pthread_attr_getstacksize(&attr, &tThStackSize);

                nRet = ::pthread_attr_setstacksize(&attr, m_dwStackSize);
                if( 0 != nRet)
                {
                        printf("!!TestCreateThread,3,setstacksize fail,err=%d,%s\n", errno, strerror(errno));
                }

                //======test,begin
#if 0
                size_t tThStackSize = 0;
                nRet = ::pthread_attr_getstacksize(&attr, &tThStackSize);
                if(0 != nRet)
                {
                        printf("!!TestCreateThread,4,pthread_attr_getstacksize fail,err=%d,%s\n", errno, strerror(errno));
                }

                printf("TestCreateThread,4,pthread_attr_getstacksize,tThStackSize=%d\n", tThStackSize);
#endif
                //======test,end
        }
        //add by zfq,2013.09.17,end

        BOOL BRet = FALSE;
        //        if (0 == pthread_create(&m_thread, NULL, fProc, pParam))        //del by zfq,2013.09.17
        if (0 == pthread_create(&m_thread, &attr, ThOfCreateThread, NULL))        //add by zfq,2013.09.17,加上属性
        {
                BRet = TRUE;
        }
        else
        {
                printf("!!TestCreateThread,5,pthread_create fail,err=%d,%s\n", errno, strerror(errno));
        }

        nRet = pthread_attr_destroy(&attr);
        if(0 != nRet)
        {
                printf("!!TestCreateThread,7,pthread_attr_destroy fail,err=%d,%s\n", errno, strerror(errno));
        }

        pthread_join(m_thread, NULL);
        return;
}

int main(int argc, char**argv)
{
   TestCreateThread();
   return 1;
}

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
2 [报告]
发表于 2013-09-18 17:54 |只看该作者
回复 1# zhongfangqing


    stack size不能大于ulimit -s输的系统值(上限),不能小于PTHREAD_STACK_MIN这个下限。

论坛徽章:
0
3 [报告]
发表于 2013-09-18 18:12 |只看该作者
上限是8M,我设置的2M

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
4 [报告]
发表于 2013-09-18 19:13 |只看该作者
回复 1# zhongfangqing
按照apue中说的,使用线程栈属性应当要检查是否支持
  1. Support for thread stack attributes is optional for a POSIX-conforming operating system, but is required if the system is to conform to the XSI. At compile time, you can check whether your system supports each thread stack attribute using the _POSIX_THREAD_ATTR_STACKADDR and _POSIX_THREAD_ATTR_STACKSIZE symbols. If one is defined, then the system supports the corresponding thread stack attribute. You can also check at runtime, by using the _SC_THREAD_ATTR_STACKADDR and _SC_THREAD_ATTR_STACKSIZE parameters to the sysconf function.
复制代码

论坛徽章:
0
5 [报告]
发表于 2013-09-22 09:20 |只看该作者
回复 4# 井蛙夏虫


    谢谢回复!
    这两个宏确实没有被定义,但使用命令"ulimit -s 2048"这种方式可以修改为2M,但这种方式就比较死板了,不能针对线程级别进行设置
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP