Any good examples of threading (pthread) for a C and platformio newcomer?

Hi all. I’ve been working on a LTE weather station project for a while using a RPI pico, so everything in python. I hit some limitations with the hardware, and decided to switch to a ESP32 lilygo board that has the same LTE chip.

The previous project was using _thread in python to read the weather station readings in one script, and send them out via MQTT in the other.

My understanding is that pthread is the best way to accomplish this with the new board, but I’m struggling as I can’t find a decent example.

If anyone knows a decent example, or can offer a better solution than pthread, I’d love to hear it!

Thanks

The ESP32 comes with FreeRTOS.
You can start a task with xTaskCreate() or xTaskCreatePinnedToCore().

Here is a good tutorial which utilizes both cores of the ESP32 (xTaskCreatePinnedToCore):

POSIX threads (pthreads) are not implemented in Arduino-ESP32, but as @sivar2311 said, the FreeRTOS real-time-operating-system provides equivalent functionality.

I can highly recommend the following 12-part video series by DigiKey:

1 Like

Thanks to you both @maxgerhardt and @sivar2311

I got seemingly 90% of the way there, but I’m getting some interesting issues when I compile; undefined references to each task as named in my main file, inside the task.cpp.d files in src generated for each task.

Any suggestions? Thanks

You’re just referencing your task functions as extern "C", so in which .c file are they actually implemented then?

Also there’s no need for them to have C linkage or be implemented in .c, you can write the task in normal .cpp code as well. That way, you make sure you actually have access to all the Arduino classes and functions, unlike from C.