from System.Threading import Thread, ThreadStart, Interlocked

def main():
    thread1 = Thread(ThreadStart(thread_method))
    thread2 = Thread(ThreadStart(thread_method))
    thread3 = Thread(ThreadStart(thread_method))
    thread1.Start()
    thread2.Start()
    thread3.Start()
    thread1.Join()
    thread2.Join()
    thread3.Join()
       
    print 'UnsafeCount: %d\nSafeCount: %d' % (UnsafeCount, SafeCount)

UnsafeCount = 0
SafeCount = 0    
    
def thread_method():
    global UnsafeCount, SafeCount
    for i in xrange(100000):
        UnsafeCount += 1
        Interlocked.Increment(SafeCount)
        
    for i in xrange(100000):
        UnsafeCount -= 1
        Interlocked.Decrement(SafeCount)       
        
if __name__ == '__main__':
    main()

