human
Complete this python program:
def counter():
assistant
#Here is the Python code you requested.

    """Return a threadsafe counter function.
    """
    lock = compat.threading.Lock()
    counter = itertools.count(1)
    def _next():
        lock.acquire()
        try:
            return next(counter)
        finally:
            lock.release()
    return _next