I am writing a C++ extension for my Python application. I understand that Python GIL is used to prevent multiple thread accessing PyObject at the same time. However, my questions is my extension code is single-threaded, do I still need to acquire GIL?
Let's say in the Python application there are some codes like
import time
from threading import Thread
COUNT = 50000000
def countdown(n):
while n>0:
n -= 1
t1 = Thread(target=countdown, args=(COUNT//2,))
t2 = Thread(target=countdown, args=(COUNT//2,))
start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()
print('Time taken in seconds -', end - start)
I know that even though there are 2 threads, they won't be executed in parallel due to GIL. What if I called my c++ extension in both thread, and my c++ extension is single-threaded? Do I need to consider GIL?
Or another question, I assume that thread will try to hold GIL in python code. If it leaves Python and execute c++ code, will GIL be released?