Will pthread_detach manage my memory for me?

Suppose I have the following code:

while(TRUE) {
  pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t));
  pthread_create(thread, NULL, someFunction, someArgument);
  pthread_detach(*thread);
  sleep(10);
}

Will the allocated thread free the memory allocated by malloc, or is that what I now need to do?

+3
source share
2 answers

No. pthread_create () does not know that the thread pointer passed to it was dynamically allocated. pthreads does not use this value internally; it just returns the new thread id to the caller. You do not need to dynamically highlight this value; you can pass the address of a local variable:

pthread_t thread;
pthread_create(&thread, NULL, someFunction, someArgument);
+10
source

. pthread_t .

+1

Source: https://habr.com/ru/post/1697533/


All Articles