'Construct a bunch of `n` threads running the same function `f`.
If `wait_before_exit` is True, the threads won\'t terminate until
do_finish() is called.'

def __init__(self, f, n, wait_before_exit=False):

    self.f = f
    self.n = n
    self.started = []
    self.finished = []
    self._can_exit = (not wait_before_exit)
    def task():
        tid = threading.get_ident()
        self.started.append(tid)
        try:
            f()
        finally:
            self.finished.append(tid)
            while (not self._can_exit):
                _wait()
    try:
        for i in range(n):
            start_new_thread(task, ())
    except:
        self._can_exit = True
        raise
