Particle Thread Transition with Delay

Hello,

I was trying to understand how a couple C++ programming concepts behave with particle P1 .

In a Multi-threaded application, where networking is set to SEMI_AUTO (we want to perform local hardware calculations first before calling networking stack), and given that delay in Multi-threaded with p1, will yield the control back to the loop which is in microcontrollers queue,

  • How does particle distribute CPU resources/time when main loop thread, calls for a networking thread based function, and encounters a delay in the networking thread.

Would the control transfer back to the main loop and continue execution until the delay period is over in the main loop, or this time the control will be held by the networking thread, until the entire exercise is executed.

in other words, when main loop calls for a networking loop, and networking loop has a delay, where does the program go, if no other loops/timers/interrupts are present. Would Program suspend until delay time is executed.

For example code in the main loop,

A function for connection status check calls for

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

if(particle.connected()){ }
Else if(!WiFi.connected())
{ Void tryconnect(); //in Another Header File:  }

Void tryconnect() {
  WiFi.on();
  delay(100);
  WiFi.connect();
  delay(400);
  Particle.connect();
  delay(400);
}

Particle devices run FreeRTOS which uses 1ms time slices for each thread (unless a particlular thread prevents thread switching - but connection process is not doing that).

What is a "networking loop"?

In your tryconnect() (BTW, void has no capital V, nor does else have a capital E) you'd typically not use delay() but rather waitFor() like this

bool tryconnect() {
  WiFi.on();
  WiFi.connect();
  if (!waitFor(WiFi.ready, 30000)) 
    return false;
  Particle.connect();
  if (!waitFor(Particle.connected, 30000))
    return false;
  return true;
}

Both threads would immediately yield their part of the time slice and thread switching would happen very rapidly to and fro.

1 Like

hello,

Thank you so much once again, for such a timely and accurate response. It answers all the questions I had proposed.

Thanks
Amogh

1 Like