docs: def thread_counter(finalize):
      """Modifies a thread target function, such that the number of active
      threads is counted. If the count reaches zero, a finalizer is called."""
      n_threads = 0
      lock = threading.Lock()
  
      def target_modifier(target):
          @functools.wraps(target)
          def modified_target(*args, **kwargs):
              nonlocal n_threads, lock
  
              with lock:
                  n_threads += 1
  
              return_value = target(*args, **kwargs)
  
              with lock:
                  n_threads -= 1
                  if n_threads == 0:
                      finalize()
  
              return return_value
  
          return modified_target
  
      return target_modifier