Use thread_group for improved exception safety (#1785).