Multicore support

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Tinus
Posts: 6
Joined: Sun Feb 14, 2021 4:53 pm

Multicore support

Post by Tinus » Sun Feb 14, 2021 5:49 pm

There is something I don't quite understand in the pi pico MicroPython SDK pdf

it says:

Code: Select all

Only one thread can be started/running at any one time, because there is no RTOS just a second core. The GIL is not
enabled so both core0 and core1 can run Python code concurrently, with care to use locks for shared data
I think that is a good thing because I assume I can do calculations on one core and run the display from the other.
However I do not know how to lock shared data or if I even should.

I also assumed that the main program runs on one core and if I start the extra thread it will run on the other core. Is that assumption correct?

User avatar
jimmo
Posts: 2236
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Multicore support

Post by jimmo » Mon Feb 15, 2021 1:57 am

Tinus wrote:
Sun Feb 14, 2021 5:49 pm
I also assumed that the main program runs on one core and if I start the extra thread it will run on the other core. Is that assumption correct?
Yes that's exactly right. There's no "OS" as such, so no pre-emptive multitasking or thread switching etc. MicroPython just provides one thread per core (or more accurately, a "main" thread on the first core and an optional second thread).
Tinus wrote:
Sun Feb 14, 2021 5:49 pm
However I do not know how to lock shared data or if I even should.
Yes, you should. Unlike other Python implementations (including other MicroPython ports), MicroPython on the Pico doesn't use the "Global Interpreter Lock" so both threads run completely concurrently.

The _thread module provides a lock primitive (see _thread.allocate_lock() ) -- https://docs.python.org/3.5/library/_th ... le-_thread

Tinus
Posts: 6
Joined: Sun Feb 14, 2021 4:53 pm

Re: Multicore support

Post by Tinus » Mon Feb 15, 2021 1:29 pm

Thank you very much.

Post Reply