MongoDB: When shutting down threads, release lock before attempting to join.

Fixes potential deadlock when exiting.