added hosekeeping of keys and destructor call when the pthread ends