Added thread locking for sorting queue's