Skip to content

request: simpler wrappers or use std::thread for multithreading #208

@dsyleixa

Description

@dsyleixa

for RP2040 boards (Arduino, Adafruit, Raspberry Pi Pico) and nano33 BLE,
because the current multithreading implementation is very overcomplicated and hard to understand, other than std::thread or pthread:
Will it be possible to provide simpler wrappers for multithreading to have a syntax more like pthread.h or std::thread?
e.g., for a simplified MT blink example:

#include <thread>

using namespace std;

volatile static int STOP_FLAG=0; // semaphore for thread control

// The function we want to execute on the new thread.
void blink( int pin )
{
     pinMode( pin, OUTPUT );
     while( STOP_FLAG==0 )
     {
          digitalWrite(pin, HIGH);
          Serial.println("blink thread: pin "+String(pin)+" ON");
          delay(500);
          digitalWrite(pin, LOW);
          Serial.println("blink thread: pin "+String(pin)+" OFF");
          delay(500);
     }
     Serial.println("blink thread: terminated");
}

int main()
{
    Serial.println("main: starting thread(s)");
    // Constructs the new thread and runs it. Does not block execution.
    thread t1( blink, LED_BUILTIN );

    // Do other things...
    delay(5000);        // let all things run for 5sec     

    Serial.println("main: signal thread(s) to stop");
    STOP_FLAG=1;  // now set stop flag=true to signal the blink thread to stop it's infinite loop

    Serial.println("main: waiting for thread(s) to finish");
    t1.join(); //  wait for the thread(s) to finish execution, therefore blocks it's own execution.

    Serial.println("main: finished");
}

instead of main() one also might do the thread creation in the common Arduino setup() function and then do other things repeatedly, additionally, in the standard loop() as usual.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions