I am trying to run the following example from the Threads Tutorial:
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
void threadFunction(void);
Thread thread("testThread", threadFunction);
volatile int counter = 0;
unsigned long lastReport = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
if (millis() - lastReport >= 1000) {
lastReport = millis();
Serial.printlnf("counter=%d", counter);
}
}
void threadFunction(void) {
while(true) {
counter++;
}
// You must not return from the thread function
}
The device is an LTE E-Series with OS v1.4.0.
I am able to locally compile and flash without any issues. The problem is that the status LED goes to solid white following a reboot or flash. The serial monitor gets stuck at “Polling for available serial device…”
I am able to successfully run applications that do not use Threads without any issues.
UPDATE:
I was able to get it to work by starting the thread after initializing Serial in the setup function.
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
void threadFunction(void);
Thread thread;
volatile int counter = 0;
unsigned long lastReport = 0;
void setup() {
Serial.begin(9600);
thread = Thread("testThread", threadFunction);
}
void loop() {
if (millis() - lastReport >= 1000) {
lastReport = millis();
Serial.printlnf("counter=%d", counter);
}
}
void threadFunction(void) {
while(true) {
counter++;
}
// You must not return from the thread function
}