分享

pthread之线程堆栈

 wusiqi111 2016-10-12

先来讲说线程内存相关的东西,主要有下面几条:

  1. 进程中的所有的线程共享相同的地址空间。
  2. 任何声明为static/extern的变量或者堆变量可以被进程内所有的线程读写。
  3. 一个线程真正拥有的唯一私有储存是处理器寄存器。
  4. 线程栈可以通过暴露栈地址的方式与其它线程进行共享。

     有大数据量处理的应用中,有时我们有必要在栈空间分配一个大的内存块或者要分配很多小的内存块,但是线程的栈空间的最大值在线程创建的时候就已经定下来了,如果栈的大小超过个了个值,系统将访问未授权的内存块,毫无疑问,再来的肯定是一个段错误。可是没办法,你还是不得不分配这些内存,于是你开会为分配一个整数值而动用malloc这种超级耗时的操作。当然,在你的需求可以评估的情况下,你的需求还是可以通过修改线程的栈空间的大小来改变的。

下面的我们用pthread_attr_getstacksize和pthread_attr_setstacksize的方法来查看和设置线程的栈空间。

/*
 * =====================================================================================
 *
 *       Filename:  thread_stack.c
 *
 *    Description:  thread stack size 
 *
 *        Version:  1.0
 *        Created:  2012年06月15日 10时53分37秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <pthread.h>
#include <error.h>
#include <string.h>

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

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

    printf( "Get 7M 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)
        perror("Create attr");

    status = pthread_attr_setdetachstate (
        &thread_attr, PTHREAD_CREATE_DETACHED);

    if (status != 0)
        perror("Create attr");

    //通常出现的问题之一,下面的宏没有定义
//#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    //得到当前的线程栈大小
    status = pthread_attr_getstacksize (&thread_attr, &stack_size);
    if (status != 0)
        perror("Create attr");

    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)
        perror("Create attr");

       
    //得到当前的线程栈的大小
    status = pthread_attr_getstacksize (&thread_attr, &stack_size);
    if (status != 0)
       perror ("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_stack, NULL);
        if (status != 0)
            perror ( "Create thread");
    }

    getchar();
    printf ("Main exiting\n");
    pthread_exit (NULL);
    return 0;
}

/home/horst/horstdemo $ ./thread_stack
Default stack size is 8388608; minimum is 16384
Default stack size is 16777216; minimum is 16384
The thread is here
The thread is here
The thread is here
The thread is here
The thread is here
Get 7M Memory!!!
Get More Memory!!!
Get 7M Memory!!!
Get More Memory!!!
Get 7M Memory!!!
Get More Memory!!!
Get 7M Memory!!!
Get More Memory!!!
Get 7M Memory!!!
Get More Memory!!!

Main exiting

linux debain 下命令查看栈的大小

horst@debian:~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 16382
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited


用下面的命令改变进程栈空间:
ulimit -s 16392

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多