当前位置: 首页 > news >正文

360官网广西seo搜索引擎优化

360官网,广西seo搜索引擎优化,期货网站做模拟,wordpress关闭订阅最近公司里遇到一个线程栈大小的问题,借此机会刚好学习一下这个线程栈大小相关的函数。如果公司里用的还是比较老的代码的话,都是用的 pthread 库支持线程的,而不是 c11 里的线程类。主要有两个相关函数:pthread_attr_setstacksiz…

        最近公司里遇到一个线程栈大小的问题,借此机会刚好学习一下这个线程栈大小相关的函数。如果公司里用的还是比较老的代码的话,都是用的 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 nolonger 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/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense 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 systemwill 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 atonce.  This also takes care of clearing the bytes which are notinternally 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 toosmall pthread_attr_t definition.  The struct looked likethis:  */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 atonce.  This also takes care of clearing the bytes which are notinternally 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 theold 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.15wanjia.com/news/32761.html

相关文章:

  • 垫江集团网站建设网店推广的渠道有哪些
  • 网站建设与管理专业好吗网址百度刷排名
  • 哪些网站可以做移动端模板长春seo排名扣费
  • asp网站源码说明360营销推广
  • 珠海定制网站制作北京网站排名seo
  • 企业文化vi设计seo入口
  • 榆林市网站建设近一周热点新闻
  • 建设网站专业网络营销的概念
  • 做养生的网站多吗制作网页链接
  • 网站未备案推广app
  • 重庆网站建设科技公司软文营销案例文章
  • 网站帮助页面设计百度seo和sem
  • 电商品牌网站开发运营免费的云服务器有哪些
  • 网站自动下注程序需要怎么做谷歌搜索引擎营销
  • 免费申请网站官网百度一下首页网址
  • 怎么做qq靓号网站厦门网站设计公司
  • 网站开发中的3p技术泉州百度seo公司
  • wordpress中文视频插件seo职位
  • 怎么做网站树洞软文推广文案范文
  • 济南 网站优化嘉兴网站建设
  • 网站从建设到赚钱的流程用html制作淘宝网页
  • 济南制作网站公司百度手机下载安装
  • 宁波建设工程信息网百度搜索关键词排名优化推广
  • 苏州网站建设logoseo排名赚下载
  • 个人网站的成本seo排名点击软件
  • 做网站空间哪家好百度推广售后服务电话
  • 免费的源码分享网站百度seo优化软件
  • 当今做网站的流行趋势网上销售有哪些方法
  • 重庆全网推广深圳seo推广外包
  • 想开一家客服外包公司优化大师电视版