简体   繁体   中英

Confusion regarding pthread_exit freeing of automatic variables

According to this

"If a thread calls pthread_exit , though, C++ doesn't guarantee that destructors are called for all automatic variables on the thread's stack. A clever way to recover this functionality is to invoke pthread_exit at the top level of the thread function by throwing a special exception".

This is followed by this accompanying code

class ThreadExitException
{

public:
ThreadExitException (void* return_value)
                    : thread_return_value_ (return_value) { }

void* DoThreadExit ()
{
 pthread_exit (thread_return_value_);
}

private:
void* thread_return_value_;

};

void do_some_work ()
{
while (1) 
{
 /* Do some useful things here...*/
 if (should_exit_thread_immediately ())
 throw ThreadExitException (/* thread’s return value = */ NULL);
}
}

void* thread_function (void*)
{
 try 
 {
  do_some_work ();
 }
catch (ThreadExitException ex) 
{
 ex.DoThreadExit ();
}

return NULL;
}

From what i understand... If pthread_exit is called there is a possiblilty that destructors for all automatic variables of the stack won't be called. And so we use Exception Handling to be sure destructors are called for all the variables. (Quoted from the explanation : "By throwing a ThreadExitException instead of calling pthread_exit directly, the exception is caught in the top-level thread function, all local variables on the thread's stack will be destroyed properly as the exception percolates up."

According to the man page of pthread_exit

"Any clean-up handlers established by pthread_cleanup_push(3) that have not yet been popped, are popped (in the reverse of the order in which they were pushed) and executed. If the thread has any thread-specific data, then, after the clean-up handlers have been executed, the corresponding destructor functions are called, in an unspecified order ".

This says that destructrutors are called for the functions, so automatic variables would be freed?. Then why is there a necessity to use Exception Handling as demonstrated by the code?

Thread specific data is something different from what you understand. Thread specific data is global data that has been set up so that each thread gets a separate copy of the global data. So thread specific data doesn't live on the stack as you are assuming.

See here for more (and a pthread example).

No, it says that destructor functions are called for thread-specific data. This is completely different from C++ destructors being called on automatic variables in the thread call stack.

See man pthread_key_create for documentation on destructor functions associated with thread-specific data.

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

[...] An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with that key, the value of the key is set to NULL, and then the function pointed to is called with the previously associated value as its sole argument. The order of destructor calls is unspecified if more than one destructor exists for a thread when it exits. [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM