pthreadのスタックサイズは制限されません。

pthreadのスタックサイズは制限されません。

私のデフォルトのスタックサイズ(ulimit -sに応じて)は8192kBなので、以下のコードを実行しようとすると自然にセグメントが発生します。また、当然、「ulimit -s 9000」を実行すると正常に動作します。ただし、「ulimit -s unlimited」を実行すると、コードsegfaultが再び発生します。どんなアイデアがありますか?

役に立つ場合は、カーネル4.19.0-6とgccバージョンのDebian 8.3.0-6を含むDebian 10を実行しています。

#include <iostream>
#include <unistd.h>
#include <cstdlib>

void* wait_exit(void*)
{
  char bob[8193*1024];
  return 0;
}

int main()
{
  pthread_t t_exit;
  int ret;
  
  if((ret = pthread_create(&t_exit,NULL,wait_exit,NULL)) !=0)
  {
    std::cout<<"Cannot create exit thread: "<<ret<<std::endl;
  }
  std::cout<<"Made thread"<<std::endl;
  sleep(5);
  return 0;
}

ベストアンサー1

「無制限」スレッドの場合、x86_64では2MiBしか使用できないため、次を参照してください。pthread_create マニュアルページ:

If the RLIMIT_STACK resource limit is set to "unlimited", a per-architecture value is used 
for the stack size.  Here is the value for a few architectures:

              ┌─────────────┬────────────────────┐
              │Architecture │ Default stack size │
              ├─────────────┼────────────────────┤
              │i386         │               2 MB │
              ├─────────────┼────────────────────┤
              │IA-64        │              32 MB │
              ├─────────────┼────────────────────┤
              │PowerPC      │               4 MB │
              ├─────────────┼────────────────────┤
              │S/390        │               2 MB │
              ├─────────────┼────────────────────┤
              │Sparc-32     │               2 MB │
              ├─────────────┼────────────────────┤
              │Sparc-64     │               4 MB │
              ├─────────────┼────────────────────┤
              │x86_64       │               2 MB │
              └─────────────┴────────────────────┘

おすすめ記事