<gh_stars>1-10
import threading
from time import sleep
from random import random

counter = 0


def randsleep(): return sleep(0.1 * random())


def incr(n):
    global counter
    for count in range(n):
        current = counter
        randsleep()
        counter = current + 1
        randsleep()


# fix race condition with locking the counter variable
incr_lock = threading.Lock()


def incr(n):
    global counter
    for count in range(n):
        with incr_lock:
            current = counter
            randsleep()
            counter = current + 1
            randsleep()


n = 7
t1 = threading.Thread(target=incr, args=(n, ))
t2 = threading.Thread(target=incr, args=(n, ))
t1.start()
t2.start()
t1.join()
t2.join()
print(f'Counter: {counter}')
