Global Variables with threading in python
Question: By threading a process as in this code:

    
    
    import threading
    import time
    
    def loop():
        global bbcount
        bbcount = 0
        while True:
            bbcount += 1
            print "This is a Loop"
            time.sleep(1)
    
    if __name__ == '__main__':
    
        t = threading.Thread(target=loop)
        t.start()
        while True:
            user_input = raw_input("Please enter a value:")
            print "User input: " + user_input
            print "Current count: " + str(bbcount)
        t.join()
    

Do I run the risk of bbcount being corrupted by trying to print a partially
updated/incremented memory space while being incremented the loop?

I thought about this after reading: Since all threads of a process share the
same global variables, a problem arises with synchronization of access to
global variables. For example, let's assume you have a global variable X and
two threads A and B. Let's say threads A and B will merely increment the value
of X. When thread A begins execution, it copies the value of X into the
registers and increments it. Before it gets a chance to write the value back
to memory, this thread is suspended. The next thread starts, reads the same
value of X that the first thread read, increments it and writes it back to
memory. Then, the first thread finishes execution and writes its value from
the register back to memory. After these two threads finish, the value of X is
incremented by 1 instead of 2 as you would expect.

from: <http://www.linuxjournal.com/article/3138>


Answer: Because of GIL, this program would run expectedly.

More about GIL: <https://en.wikipedia.org/wiki/Global_Interpreter_Lock>

So the global variables will be thread-safe editing in a short time.

