pthread_attr_getstacksize 问题

news2025/7/22 21:24:54

        最近公司里遇到一个线程栈大小的问题,借此机会刚好学习一下这个线程栈大小相关的函数。如果公司里用的还是比较老的代码的话,都是用的 pthread 库支持线程的,而不是 c++11 里的线程类。主要有两个相关函数:pthread_attr_setstacksize() 和 pthread_attr_getstacksize()。

        下面看一下简单的例子:

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>

#define handle_error_en(en, msg) \
	   do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error(msg) \
	   do { perror(msg); exit(EXIT_FAILURE); } while (0)

static void *thread_start(void *arg)
{	
	size_t stack_size = 0;
	pthread_attr_t attr;
	int ret = pthread_attr_init(&attr);
	if(ret != 0)
	{
		handle_error_en(ret, "pthread_attr_init");
	}
   ret = pthread_attr_getstacksize(&attr, &stack_size);
   if (ret != 0)
   {
	   handle_error_en(ret, "pthread_attr_setstacksize");
   }
	printf("stack_size = %lu\n", stack_size);
   return 0;
}

int main(int argc, char *argv[])
{
   int s, tnum, opt, num_threads;
   pthread_t thread_id;
   pthread_attr_t attr;
   int stack_size;
   void *res;

   /* The "-s" option specifies a stack size for our threads */
   stack_size = -1;
   while ((opt = getopt(argc, argv, "s:")) != -1) 
   {
	   switch (opt) 
	   {
	   case 's':
		   stack_size = strtoul(optarg, NULL, 0);
		   break;

	   default:
		   fprintf(stderr, "Usage: %s [-s stack-size] arg...\n", argv[0]);
		   exit(EXIT_FAILURE);
	   }
   }

   /* Initialize thread creation attributes */
   s = pthread_attr_init(&attr);
   if (s != 0)
   {
	   handle_error_en(s, "pthread_attr_init");
   }

   if (stack_size > 0) 
   {
	   s = pthread_attr_setstacksize(&attr, stack_size);
	   if (s != 0)
	   {
		   handle_error_en(s, "pthread_attr_setstacksize");
	   }
   }


	printf("set stack size %lu\n", stack_size);
   s = pthread_create(&thread_id, &attr, &thread_start, &thread_id);
   if (s != 0)
   {
	   handle_error_en(s, "pthread_create");
   }

   /* Destroy the thread attributes object, since it is no
	  longer needed */
   s = pthread_attr_destroy(&attr);
   if (s != 0)
   {
	   handle_error_en(s, "pthread_attr_destroy");
   }

   s = pthread_join(thread_id, &res);
   if (s != 0)
   {
	   handle_error_en(s, "pthread_join");
   }

   exit(EXIT_SUCCESS);
}

代码里设置了 512 Kb 大小的线程栈,而用 pthread_attr_getstacksize()获取到的却是 8MB。为什么呢?其实这个 8388608 是系统默认的线程栈大小,可以用 ulimit -a 查看相关信息:

  

找到 pthread_attr_getstacksize 的源码看一下,大概就知道它为什么是这个返回值了

/* Copyright (C) 2002-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */
#include "pthreadP.h"
#include <shlib-compat.h>
int
__pthread_attr_getstacksize (const pthread_attr_t *attr, size_t *stacksize)
{
  struct pthread_attr *iattr;
  iattr = (struct pthread_attr *) attr;
  size_t size = iattr->stacksize;
  /* If the user has not set a stack size we return what the system
     will use as the default.  */
  if (size == 0)
    {
      lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
      size = __default_pthread_attr.internal.stacksize;
      lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
    }
  *stacksize = size;
  return 0;
}
versioned_symbol (libc, __pthread_attr_getstacksize,
                  pthread_attr_getstacksize, GLIBC_2_34);
#if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1, GLIBC_2_34)
compat_symbol (libpthread, __pthread_attr_getstacksize,
               pthread_attr_getstacksize, GLIBC_2_1);
#endif

 可以看到如果是用户没有设置栈大小的话,就会返回系统默认值,即 8MB。但上面的代码中已经调用 pthread_attr_setstacksize() 设置栈大小了,为什么还会返回默认值呢?仔细看 pthread_attr_getstacksize 的实现,它返回的栈大小其实是从入参 attr 里取的 stacksize 值,所以回到我们的代码中,我们是用 pthread_attr_init(&attr) 来初始化 attr 的,所以返回的也就是这个初始化后 attr 里的 stacksize 值。可以看看 pthread_attr_init 的源码:

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "pthreadP.h"
#include <shlib-compat.h>
struct pthread_attr *__attr_list;
int __attr_list_lock = LLL_LOCK_INITIALIZER;
int
__pthread_attr_init (pthread_attr_t *attr)
{
  struct pthread_attr *iattr;
  ASSERT_TYPE_SIZE (pthread_attr_t, __SIZEOF_PTHREAD_ATTR_T);
  ASSERT_PTHREAD_INTERNAL_SIZE (pthread_attr_t, struct pthread_attr);
  /* Many elements are initialized to zero so let us do it all at
     once.  This also takes care of clearing the bytes which are not
     internally used.  */
  memset (attr, '\0', __SIZEOF_PTHREAD_ATTR_T);
  iattr = (struct pthread_attr *) attr;
  /* Default guard size specified by the standard.  */
  iattr->guardsize = __getpagesize ();
  return 0;
}
libc_hidden_def (__pthread_attr_init)
versioned_symbol (libc, __pthread_attr_init, pthread_attr_init, GLIBC_2_1);
#if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_1)
int
__pthread_attr_init_2_0 (pthread_attr_t *attr)
{
  /* This code is specific to the old LinuxThread code which has a too
     small pthread_attr_t definition.  The struct looked like
     this:  */
  struct old_attr
  {
    int detachstate;
    int schedpolicy;
    struct sched_param schedparam;
    int inheritsched;
    int scope;
  };
  struct pthread_attr *iattr;
  /* Many elements are initialized to zero so let us do it all at
     once.  This also takes care of clearing the bytes which are not
     internally used.  */
  memset (attr, '\0', sizeof (struct old_attr));
  iattr = (struct pthread_attr *) attr;
  iattr->flags |= ATTR_FLAG_OLDATTR;
  /* We cannot enqueue the attribute because that member is not in the
     old attribute structure.  */
  return 0;
}
compat_symbol (libc, __pthread_attr_init_2_0, pthread_attr_init, GLIBC_2_0);
#endif

就是把入参 attr 给 memset 了一下,所以在 pthead_attr_getstacksize() 里,size = iattr->stacksize是为 0 的,所以 pthead_attr_getstacksize 取到的就是默认的栈大小。那要真正取到当前线程的 stacksize 怎么操作呢?使用 pthread_getattr_np() 这个函数,取指定线程的 attr,如:

static void *thread_start(void *arg)
{	
	size_t stack_size = 0;
	pthread_attr_t attr;
	int ret = pthread_getattr_np(pthread_self(), &attr);
	if(ret != 0)
	{
		handle_error_en(ret, "pthread_attr_init");
	}
   ret = pthread_attr_getstacksize(&attr, &stack_size);
   if (ret != 0)
   {
	   handle_error_en(ret, "pthread_attr_setstacksize");
   }
	printf("current thread stack_size = %lu\n", stack_size);
   return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1157065.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android 10-11适配外部存储方案

Android Api 29 对文件和文件夹进行了重大更改。不允许使用外部存储&#xff0c;如下方法&#xff1a; Environment.getExternalStorageDirectory() /mnt/sdcard Environment.getExternalStoragePublicDirectory(“test”) /mnt/sdcard/test 只能使用内部存储 getExterna…

安防监控项目---web网页通过A9控制Zigbee终端节点的风扇

文章目录 前言一、zigbee的CGI接口二、请求线程和硬件控制三、现象展示总结 前言 书接上期&#xff0c;我们可以看一下前面的功能设计的部分&#xff0c;网页端的控制还有一个&#xff0c;那就是通过网页来控制zigbee上的风扇节点&#xff0c;这部分的工作量是相当大的&#x…

管理类联考——英语二——阅读篇——题材:心理

文章目录 2013年&#xff0c;Text 3——题材&#xff1a;心理细节题&#xff08;难&#xff09;细节题——排除法细节题细节题观点态度题 2015 年&#xff0c;Text 1——题材&#xff1a;心理细节题细节题推断题词义句意题细节题 2019 年&#xff0c;Text 1——题材&#xff1a…

八、W5100S/W5500+RP2040树莓派Pico<DNS>

文章目录 1 前言2 协议简介2.1 什么是DNS2.2 DNS的优点2.3 DNS工作原理2.4 应用场景 3 WIZnet以太网芯片4 DNS网络设置示例概述以及使用4.1 流程图4.2 准备工作核心4.3 连接方式4.4 主要代码概述4.5 烧录验证 5 注意事项6 相关链接 1 前言 为了更好地支持应用程序的性能和可用性…

RuoYi若依源码分析1 - security

Security springsecurity配置文件夹 security springsecurity总配置类 SecurityConfig.java SecurityConfig 总配置分析 首先看一下总配置&#xff0c;我们可以从总配置项里面大体的总结出springsecurity鉴权在ruoyi框架里面是如何执行的 自动装配关键处理类以及过滤器等…

爬虫 | 【实践】百度搜索链接爬取,生成标题词云 | 以“AI换脸”为例

目录 &#x1f4da;链接爬取 &#x1f407;流程梳理 &#x1f407;代码实现 &#x1f407;结果 &#x1f4da;词云生成 &#x1f407;代码实现 &#x1f407;结果 &#x1f4da;链接爬取 &#x1f407;流程梳理 总体流程是&#xff1a;构建搜索链接 -> 发送HTTP请求…

解决Linux Debian12系统中安装VirtualBox虚拟机无法使用USB设备的问题

Debian12系统中安装VirtualBox&#xff0c;再VirtualBox虚拟机中无法使用 USB设备。如下图所示&#xff1a; 解决方法如下&#xff1a; 1.安装 Virtualbox增强功能。如下图所示&#xff1a; 2.添加相关用户、用户组&#xff08; Virtualbox 装完成后会有 vboxusers 和 vboxs…

初学编程学什么语言,中文编程系统化教程课程之自定义图形窗口自定义标题栏编程,零基础学编程轻松学编程

初学编程学什么语言&#xff0c;中文编程系统化教程课程之自定义图形窗口自定义标题栏编程&#xff0c;零基础学编程轻松学编程 该编程工具开发的系列管理软件 编程系统化课程总目录及明细&#xff0c;零基础学编程视频教程&#xff0c;点击进入了解详情。 https://blog.csdn.n…

无法ping通ECS服务器公网IP的排查方法

无法ping通ECS实例的原因较多&#xff0c;您可以参考九河云编辑的文章进行排查。 问题现象 本地客户端无法ping通目标ECS实例公网IP&#xff0c;例如&#xff1a; 本地客户端为Linux系统&#xff0c;ping目标ECS实例公网IP时无响应&#xff0c;如下所示&#xff1a; 本地客…

Java日志组件之三Log4j2漏洞剖析及重现

一、前言 这一篇我们来介绍一下史上第二严重的安全漏洞是个什么情况&#xff0c;原理是什么&#xff0c;如何重现。 二、Log4j2 Lookup机制 Log4j2 Lookup机制最重要的功能就是提供一个可扩展的方式让你可以添加某些特殊的值到日志中。你调用logger.info(name);这样的语句&a…

【赠书第1期】零基础学无人机航拍与短视频后期剪辑实战教程

文章目录 前言 1 购买前须知 2 准备工作 3 飞行控制 4 拍摄技巧 5 安全提示 6 推荐图书 7 粉丝福利 前言 随着科技的不断发展&#xff0c;无人机已经不再是军事装备的专属&#xff0c;它已经成为了消费级无人机的主流产品。作为国内领先的无人机生产商&#xff0c;大疆…

【腾讯云 HAI域探秘】StableDiffusionWebUI一小时搞定100张设计图

目录 前言一、选择 HAI部署的优势二、HAI 搭建AI绘图服务实现思路三、生成设计图操作流程1、新建HAI应用2、StableDiffusionWebUI&#xff08;1&#xff09;功能介绍&#xff08;2&#xff09;页面转中文&#xff08;3&#xff09;线稿生成图 四、部署StableDiffusionWebUI服务…

通讯网关软件033——利用CommGate X2OPC实现PI数据写入OPC Server

本文推荐利用CommGate X2OPC实现从PI服务器读取数据并写入OPC Server。CommGate X2OPC是宁波科安网信开发的网关软件&#xff0c;软件可以登录到网信智汇(http://wangxinzhihui.com)下载。 【案例】如下图所示&#xff0c;实现从PI实时数据库获取数据并写入OPC Server。 【解决…

maven环境变量的配置

windows系统 1. win键 r&#xff0c;输入sysdm.cpl打开系统属性界面&#xff0c;选择高级栏目&#xff0c;点击环境变量菜单打开环境变量界面。 2. 选择系统变量下的新建菜单&#xff0c;变量名输入MAVEN_HOME&#xff0c;变量值输入maven的安装目录&#xff0c;例如&#xff…

CNN卷积神经网络模型的GPU显存占用分析

一、参考资料 浅谈深度学习:如何计算模型以及中间变量的显存占用大小 如何在Pytorch中精细化利用显存 二、相关介绍 0. 预备知识 为了方便计算&#xff0c;本文按照以下标准进行单位换算&#xff1a; 1 G 1000 MB1 M 1000 KB1 K 1000 Byte1 B 8 bit 1. 模型参数量的计…

基于构件的开发(CBD)

基于构件的开发&#xff08;Component-Based Development&#xff0c;简称CBD&#xff09;或基于构件的软件工程&#xff08;Component-Based Software Engineering&#xff0c;简称CBSE&#xff09;是一种软件开发新范型&#xff0c;它是在一定构件模型的支持下&#xff0c;复…

FPGA与ASIC有什么差异?二者该如何选用?

前言 对于一个数字电路的新手来说&#xff0c;这可能是会经常遇到的一个问题&#xff1a;FPGA和ASIC之间的区别是什么? 接下来本文将尝试讲解 “什么是FPGA&#xff1f;” 和 “什么是ASIC&#xff1f;”&#xff0c;然后讲述一些关于FPGA和ASIC的问题&#xff0c;例如它们之间…

【【FIFO to multiplier to RAM的 verilog 代码 和 testbnench 】】

FIFO to multiplier to RAM的 verilog 代码 和 testbnench 只完成了单个数据的传输 大数据需要修改 tb 或者基本连线 FIFO.v //synchronous fifo module FIFO_syn #(parameter WIDTH 16, // the fifo wideparameter DEPTH 1024, …

【MATLAB】全网唯一的7种信号分解+ARIMA联合的时序预测算法全家桶

有意向获取代码&#xff0c;请转文末观看代码获取方式~ 大家吃一顿火锅的价格便可以拥有7种信号分解ARIMA组合的时序预测算法&#xff0c;绝对不亏&#xff0c;知识付费是现今时代的趋势&#xff0c;而且都是我精心制作的教程&#xff0c;有问题可随时反馈~也可单独获取某一算…

【三方登录-Apple】iOS 苹果授权登录(sign in with Apple)之开发者配置一

记录一下sign in with Apple的开发者配置 前言 关于使用 Apple 登录 使用“通过 Apple 登录”可让用户设置帐户并使用其Apple ID登录您的应用程序和关联网站。首先使用“使用 Apple 登录”功能启用应用程序的App ID 。 如果您是首次启用应用程序 ID 或为新应用程序启用应用程序…