首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

请问,设置线程栈大小,设置成功,但是不起作用

2013-09-26 
请教,设置线程栈大小,设置成功,但是不起作用//各位牛哥、牛姐、牛弟、牛妹://下面这坨代码,目的是想设置新线

请教,设置线程栈大小,设置成功,但是不起作用
//各位牛哥、牛姐、牛弟、牛妹:
//下面这坨代码,目的是想设置新线程的栈大小,但是不起作用。在下环境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;
} 线程?栈
[解决办法]
pthread_attr_init()会去初始化线程的属性参数,所以你拿到的还是系统默认的。
解决的方法是,不要调用这个函数。在create 的时候,用一个全局的 attr变量。后面去拿的时候,按照那个全局的来拿。
http://blog.csdn.net/caspiansea/article/details/11509243

热点排行