- 论坛徽章:
- 0
|
本帖最后由 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, ¶m);
param.__sched_priority = nPriority;
pthread_attr_setschedparam(&attr, ¶m);
#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;
} |
|