zoukankan      html  css  js  c++  java
  • pthread之线程堆栈

    转:http://blog.csdn.net/horstlinux/article/details/7666032

    //先来讲说线程内存相关的东西,主要有下面几条:
    //进程中的所有的线程共享相同的地址空间。
    //任何声明为static/extern的变量或者堆变量可以被进程内所有的线程读写。
    //一个线程真正拥有的唯一私有储存是处理器寄存器。
    //线程栈可以通过暴露栈地址的方式与其它线程进行共享。
    //     有大数据量处理的应用中,有时我们有必要在栈空间分配一个大的内存块或者要分配很多小的内存块,但是线程的栈空间的最大值在线程创建的时候就已经定下来了,如果栈的大小超过个了个值,系统将访问未授权的内存块,毫无疑问,再来的肯定是一个段错误。可是没办法,你还是不得不分配这些内存,于是你开会为分配一个整数值而动用malloc这种超级耗时的操作。当然,在你的需求可以评估的情况下,你的需求还是可以通过修改线程的栈空间的大小来改变的。
    //
    //下面的我们用pthread_attr_getstacksize和pthread_attr_setstacksize的方法来查看和设置线程的栈空间。
    
    #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
    ");
        //栈大小为8M,如果直接分配8M的栈空间,会出现段错误 ,因为栈中还有其它的
        //变量要放署
        char p[1024 * 1024 * 7];
        int i = 1024 * 1024 * 7;
        while (i--)
            p[i] = 3;
    
        printf("Get 7M Memory
    ");
    
        //分配更多的内存(如果分配1024*1020*512的话就会出现段错误)
        char p2[1024 * 1020 + 256];
        memset(p2, 0, sizeof(char) * (1024 * 1020 + 256));
    
        printf("Get More Memory!!!
    ");
        return NULL ;
    }
    
    int main(int argc, char ** argv)
    {
        pthread_t thread_id;
        pthread_attr_t thread_attr;
        size_t stack_size;
        int err;
    
        err = pthread_attr_init(&thread_attr);
        if (err != 0)
            perror("Create attr");
    
        //设置分离状态
        err = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
        if (err != 0)
            perror("Create attr");
    
        //通常出现的问题之一,下面的宏没有定义
        //#ifdef _POSIX_THREAD_ATTR_STACKSIZE
        //得到当前的线程栈大小
        err = pthread_attr_getstacksize(&thread_attr, &stack_size);
        if (err != 0)
            perror("Create attr");
    
        printf("Default stack size is %u; minimum is %u
    ", stack_size,
                PTHREAD_STACK_MIN);
    
        //设置当前的线程的大小
        err = pthread_attr_setstacksize(&thread_attr, PTHREAD_STACK_MIN * 1024);
        if (err != 0)
            perror("Create attr");
    
        //得到当前的线程栈的大小
        err = pthread_attr_getstacksize(&thread_attr, &stack_size);
        if (err != 0)
            perror("Get stack size");
    
        printf("Default stack size is %u; minimum is %u
    ", stack_size,
                PTHREAD_STACK_MIN);
        //#endif
        int i = 5;
        //分配5个具有上面的属性的线程体的属性的线程体
        while (i--) {
            err = pthread_create(&thread_id, &thread_attr, thread_stack, NULL );
            if (err != 0)
                perror("Create thread");
        }
    
        getchar();
        printf("Main exiting
    ");
    
        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

  • 相关阅读:
    2021.07.11 【ABAP随笔】采购订单Message输出打印
    项目管理43210学习笔记
    Rails跳过回调方法
    树莓派接USB温湿度传感器(python)
    Visual Studio2019安装时报错Microsoft.Net.4.7.2.FullRedist的解决方法
    微信浏览器中H5使用wx-open-launch-app打开第三方APP
    Linux系统使用qq邮箱在线发送邮件
    LRU工程实现源码(一):Redis 内存淘汰策略
    Android Studio解决Build Log出现乱码问题
    git新拉取项目
  • 原文地址:https://www.cnblogs.com/liulipeng/p/3554739.html
Copyright © 2011-2022 走看看