docs: explain optional global-interpreter-lock
Documentation URL
https://micropython.org/
Description
I saw this on the homepage:
multithreading via the "_thread" module, with an optional global-interpreter-lock (still work in progress, only available on selected ports)
My question:
So by default micropython does not have GIL? then does it use native multithreading instead of green thread? E.g on a Linux multicore machine?
And how to do mutex between different native threads?
Code of Conduct
Yes, I agree
Multi-threading support: core, unix and cc3200
This PR adds multi-threading support to MicroPython in the form of the _thread module. The work has been generously sponsored by Pycom (https://www.pycom.io). The parts that make this up are:
- core: separating out the thread specific state from
mp_state_ctxinto an individual structure, accessed viaMP_STATE_THREAD - core: the NLR pointer is now per-thread (for x86, x86-64 and thumb archs only)
- core: thread-safe garbage collector (using a global mutex)
- core: thread-safe qstrs (using a global mutex)
- core: addition of py/mpthread.h: abstraction of threading primitive that need to be implemented by a given port
- core: addition of py/modthread.c to implement the
_threadmodule in a port-agnostic way - core: MICROPY_PY_THREAD option to enable the
_threadmodule - core: MICROPY_PY_THREAD_GIL option to enable the GIL (threading is still possible without the GIL but one must ensure not to access mutable structures (eg list) from different threads at the same time)
- tests: thread tests in tests/thread
- unix: implementation of threading using pthreads (see unix/mpthreadport.c)
- cc3200: implementation of threading using FreeRTOS threads (see cc3200/mpthreadport.c)
The GIL is not optimised (it releases/acquires on each pending exception check in the VM) but it works correctly.
To test on unix (requires pthread library):
cd unix
make
cd ../tests
./run-tests -d thread