日期:2014-05-16  浏览次数:20662 次

请教,设置线程栈大小,设置成功,但是不起作用
//各位牛哥、牛姐、牛弟、牛妹:
//下面这坨代码,目的是想设置新线程的栈大小,但是不起作用。在下环境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);