c++ - 为将来的 QThreadPool 线程和/或 pthread_create 调用设置默认堆栈大小

标签 c++ c pthreads stack qtconcurrent

我正在使用 QtConcurrent/QThreadPool,它创建的线程的堆栈大小在 Mac OS X 10.8 (512kB) 上太小,但在 CentOS 5.9 (10MB) 上很好。

我希望有一种解决方法,可以为新线程的堆栈大小设置一些进程范围的默认值。我暂时只关心类 POSIX 系统(Mac 和 Linux),而不关心 Windows。

我的问题是如何在运行程序之前从程序内部或通过环境变量或其他方式设置新 QThreadPool 线程的默认堆栈大小?

通过为pthread_create创建的线程设置默认值来实现效果的答案也将很有用。

<小时/>

我正在尝试解决这个问题:

我的预感是 Qt 在幕后使用 pthreads,如果我可以更改 pthreads 的默认堆栈大小,我的问题可能会得到解决。

pthread 文档说:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void*), void *arg);

DESCRIPTION

The pthread_create() function is used to create a new thread, with attributes specified by attr, within a process. If attr is NULL, the default attributes are used. etc...

我知道如何获取默认值:

   size_t stacksize;
   pthread_attr_t attr;
   pthread_attr_init(&attr);
   pthread_attr_getstacksize (&attr, &stacksize);

但我还没有找到设置默认值的方法 - 只能通过将 pthread_attr_t 传递给 pthread_create 来设置大小。

有办法设置默认值吗?

是否有一些环境变量或其他方法来影响默认值?

最佳答案

运行此代码以扩展进程堆栈大小。

对于 Mac OS X 和 Linux:

#include <sys/resource.h>
#include <sys/types.h>
#include <sys/time.h>

...
rlimit limits;
getrlimit(RLIMIT_STACK, &limits);
limits.rlim_cur = <new process stack size (up to limits.rlim_max)>;
setrlimit(RLIMIT_STACK, &limits);
...

参见Customizing Process Stack Size
getrlimit(2) - Linux manual page对于 Linux 和 Darwin 类似

关于c++ - 为将来的 QThreadPool 线程和/或 pthread_create 调用设置默认堆栈大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17831047/

相关文章:

c++ - C++中的结构和类有什么区别?

c++ - 给定情况下好的散列函数是什么?

c++ - 外部 C 和结构方法

c - 使用 memcpy 后无法打印指针

C 程序可以编译,但执行后立即终止

控制线程结束的顺序

c++ - 为什么 boost this_thread::interrupt 可以在没有 try-catch 的情况下工作?

我可以通过最少的复制从 Unbounded_String 获取 chars_ptr 吗?

php - PHP-PTHREAD工作线程不同时执行

C++ pthreads - 使用我在 C 中使用的代码给我一个转换错误