Примечание: я удалил все необходимые проверки ошибок в следующем фрагменте.
...
void *thread_function(void *arg)
{
...
pthread_exit("Hello");
}
pthread_t a_thread;
void *thread_result;
pthread_create(&a_thread, NULL, thread_function, NULL);
pthread_join(a_thread, &thread_result);
/*
int pthread_join(pthread_t th, void **thread_return);
The second argument is a pointer to a pointer that itself points to the return
value from the thread.
int pthread_exit(void *retval);
This function terminates the calling thread, returning a pointer to an object which
cannot be a local variable.
*/
Вопрос : как pthread_join заполняет переменную thread_result?
Поскольку переменная thread_result не имеет выделенного пространства для хранения информации,
если pthread_join выделяет место для thread_result, то основной поток должен
освободить ресурс, удерживаемый с помощью varable. Как видите, код не
включите ресурс освобождения thread_result. Итак, я предполагаю, что pthread_join
фактически не выделяет место для thread_result.
Теперь новый вопрос - как переменная thread_result может содержать информацию без
выделяется какое-то место?
// Update-1: добавить определение pthread_exit.
// Update-2: добавить определение функции thread_function.