Pthread stack size restrictions


Does anybody know why pthread_create() would return EINVAL for certain thread stack sizes when linking PyTorch, but not if PyTorch is not linked ?

Lets take this small example:

#include "pthread.h"

static void *thread1(void *pstVoid)
  std::cout << "Hello from pthread" << std::endl;

int main(int argc, char **argv)
  pthread_t tid1;
  pthread_attr_t pa; 
  iErr = pthread_attr_init(&pa);
  iErr = pthread_attr_setstacksize (&pa, 16*1024);

   iErr = pthread_create(&tid1, &pa, thread1, NULL);
   printf("iErr = %d  \n", iErr);

   pthread_join(tid1, NULL);


find_package(Torch REQUIRED)

add_executable(test main.cpp)
target_link_libraries(test PRIVATE torch pthread)

In this example pthread_create() returns the error EINVAL. However, if I remove torch from target_link_libraries then it works fine. Or alternatively if I change the stack to 1024x1024 then it also works.

We’re using version 1.7.1 of PyTorch.

And before anyone asks, I’m using a internal library where I work which uses pthread directly and not std::threads :smiley: Which is where I encountered this issue first.