(1) Added mutex to prevent race condition on shared data
(2) Minor tweaking of select_all method to improve performance