我正在使用 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/