previous | start | next

When are Thread Arguments deallocated?

If the caller of pthread_create passes the address of a struct allocated locally in the caller function and the caller terminates, the thread might be pointing to deallocated storage - a dangling pointer!

This can be handled either by ensuring the caller waits for the thread it created to terminate:

      pthread_join(thread, 0);
   

Or possibly by having the arguments allocated on the heap (instead of the caller's stack) and then the thread copies the arguments to local variables and deallocates the heap versions!



previous | start | next