I am reading APUE the chapter about thread specific data. I see following code snipit:
void destructor(void *);
pthread_key_t key;
pthread_once_t init_done = PTHREAD_ONCE_INIT;
void thread_init(void){
err = pthread_key_create(&key, destructor);
}
int threadfunc(void *arg){
pthread_once(&init_done, thread_init);
//...
}
I have several questions:
- I don't understand how does
key
initialize - Why does this key need destructor? And what does this destructor do?