How to properly allocate memory for the pthread stack

I am trying to control the use of the stack in my threads. To do this, I need to know the address of the thread stack, and the only way I found is to set the stack with pthread_attr_setstack().

I am currently using mmap to allocate memory:

   pthread_attr_t ptAttr;
   pthread_t pth;
   pthread_attr_init(&ptAttr);
   void *stack = mmap(NULL, stksize, PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
   pthread_attr_setstack(&ptAttr, stack, stksize);
   pthread_create(&pth,&ptAttr,threadFunc,&info);

So, first question, is this a good way to allocate memory with mmap? Are the flags correct? Should I use mallocinstead? This will work on a low resource device without virtual / swap memory.

Second question: will this memory be released automatically when the thread dies? If you are not sure, is there a way to find out if it is released?

+3
source share
1 answer

""? , ( 32- + swap), pthread_attr_setstacksize pthread_attr_setstack. , . pthread_attr_setguardsize, , , , , .

, pthread_attr_setstack, , , . mmap . SIGSEGV mprotect , . , . ( , ), .

pthread_create, , , , , , , , , ' .

:

  • MAP_SHARED. , . , , . MAP_PRIVATE.

  • . POSIX , undefined , , ( pthread_join, , , , , , ). , glibc/NPTL - , futex wake , pthread_join , NPTL , ( / ). , . pthread_attr_setstack . pthread_attr_setstacksize. pthread_attr_setstack , , .

+7

All Articles