# GIL stands for Global Interpreter Lock in Python.
# It is a mechanism used in CPython (the standard implementation of Python) to synchronize access to Python objects, preventing multiple native threads from executing Python bytecodes at once.
# Since the GIL limits the parallelism of Python code execution, it can sometimes be a bottleneck for programs that heavily rely on multithreading or multiprocessing.
# Here's a simple example of using the threading module in Python to demonstrate the effect of GIL:
import threading
counter = 0
def increment():
global counter
for _ in range(1000000):
counter += 1
# Create two threads that increment the counter
thread1 = threading.Thread(target=increment)
thread2 = threading.Thread(target=increment)
# Start the threads
thread1.start()
thread2.start()
# Wait for both threads to finish
thread1.join()
thread2.join()
# At the end, the value of the counter may not be the expected 2000000 due to the GIL.
print("Counter:", counter)