大龙的博客

常用链接

统计

最新评论

pthread之线程堆栈(1) --- 转

先来讲说线程内存相关的东西,主要有下面几条:
  1. 进程中的所有的线程共享相同的地址空间。
  2. 任何声明为static/extern的变量或者堆变量可以被进程内所有的线程读写。
  3. 一个线程真正拥有的唯一私有储存是处理器寄存器。
  4. 线程栈可以通过暴露栈地址的方式与其它线程进行共享。
     有大数据量处理的应用中,有时我们有必要在栈空间分配一个大的内存块或者要分配很多小的内存块,但是线程的栈空间的最大值在线程创建的时候就已经定下来了,如果栈的大小超过个了个值,系统将访问未授权的内存块,毫无疑问,再来的肯定是一个段错误。可是没办法,你还是不得不分配这些内存,于是你开会为分配一个整数值而动用malloc这种超级耗时的操作。当然,在你的需求可以评估的情况下,你的需求还是可以通过修改线程的栈空间的大小来改变的。

下面的我们用pthread_attr_getstacksize和pthread_attr_setstacksize的方法来查看和设置线程的栈空间。
注意:
      下面的测试代码在我自己的机子上(ubuntu6.06,ubuntu6.10,redhat 9, gentoo)通过了测试,但是很奇怪的是在我同事的机子上,无论是改变环境,还是想其它方法都不能正常的运行 。在网上查了一下,很多人也存在同样的问题,至今不知道为何。

linux线程的实现方式决定了对进程的限制同样加在了线程身上:)所以,有问题,请参见<pthread之线程栈空间(2)(进行栈)

直接看代码吧,只有一个C文件(thread_attr.c)
#include <limits.h>
#include <pthread.h>
#include "errors.h"


//线程体,在栈中分配一个大小为15M的空间,并进行读写
void *thread_routine (void *arg)
{
printf ("The thread is here\n");
//栈大小为16M,如果直接分配16M的栈空间,会出现段错误 ,因为栈中还有其它的
//变量要放署
char p[1024*1024*15];
int i=1024*1024*15;

//确定内存分配的确成功了
while(i--)
{
p[i] = 3;
}

printf( "Get 15M Memory!!!\n" );

//分配更多的内存(如果分配1024*1020*512的话就会出现段错误)
char p2[ 1024 * 1020 + 256 ];
memset( p2, 0, sizeof( char ) * ( 1024 * 1020 + 256 ) );
printf( "Get More Memory!!!\n" );
return NULL;
}

int main (int argc, char *argv[])
{
pthread_t thread_id;
pthread_attr_t thread_attr;
size_t stack_size;
int status;

status = pthread_attr_init (&thread_attr);
if (status != 0)
err_abort (status, "Create attr");

status = pthread_attr_setdetachstate (
&thread_attr, PTHREAD_CREATE_DETACHED);

if (status != 0)
err_abort (status, "Set detach");
//通常出现的问题之一,下面的宏没有定义
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
//得到当前的线程栈大小
status = pthread_attr_getstacksize (&thread_attr, &stack_size);
if (status != 0)
err_abort (status, "Get stack size");
printf ("Default stack size is %u; minimum is %u\n",
stack_size, PTHREAD_STACK_MIN);

//设置当前的线程的大小
status = pthread_attr_setstacksize (
&thread_attr, PTHREAD_STACK_MIN*1024);
if (status != 0)
err_abort (status, "Set stack size");

//得到当前的线程栈的大小
status = pthread_attr_getstacksize (&thread_attr, &stack_size);
if (status != 0)
err_abort (status, "Get stack size");
printf ("Default stack size is %u; minimum is %u\n",
stack_size, PTHREAD_STACK_MIN);
#endif
int i = 5;
//分配5个具有上面的属性的线程体
while(i--)
{
status = pthread_create (
&thread_id, &thread_attr, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
}

getchar();
printf ("Main exiting\n");
pthread_exit (NULL);
return 0;
}
看看执行过程:
dongq@DongQ_Lap ~/workspace/test/pthread_attr $ make
cc -pthread -g -DDEBUG -lrt  -o thread_attr thread_attr.c
dongq@DongQ_Lap ~/workspace/test/pthread_attr $ ./thread_attr
Default stack size is 8388608; minimum is 16384         //默认的栈大小为8M
Default stack size is 16777216; minimum is 16384      //设置后的结果为16M
The thread is here
The thread is here
The thread is here
The thread is here
The thread is here
Get 15M Memory!!!
Get More Memory!!!
Get 15M Memory!!!
Get More Memory!!!
Get 15M Memory!!!
Get 15M Memory!!!
Get More Memory!!!
Get More Memory!!!
Get 15M Memory!!!
Get More Memory!!!

Main exiting

posted on 2009-06-07 14:41 大龙 阅读(696) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理