Multithreading on AMB82-mini

I have stripped down the OTA example down to its essentials to so its just the threading part left, can this be used to run the GEN AI examples without stalling the loop?



class THREAD {
public:
  THREAD(void);
  ~THREAD(void);

  // To start THREAD firmware update process via HTTP
  void start_THREAD_threads();
private:
  static void thread1_task(const void *argument);
  static void thread2_task(const void *argument);

  static void sendPostRequest();
};
THREAD thread;

char BUFFER[1024];
uint32_t threada_id, threadb_id, threadc_id, stack_sizea, stack_sizeb;
int priorityA;

THREAD::THREAD(){};

THREAD::~THREAD(){};


void THREAD::thread1_task(const void *argument) {
  while (1) {
    Serial.println("Thread 1");
    digitalWrite(23, HIGH);
    delay(500);
    digitalWrite(23, LOW);
    delay(500);
  }
}
void THREAD::thread2_task(const void *argument) {
  while (1) {
    Serial.print("thread2");
    digitalWrite(24, HIGH);
    delay(2000);
    digitalWrite(24, LOW);
    delay(2000);
  }
}
void THREAD::start_THREAD_threads() {
  priorityA = osPriorityNormal;
  stack_sizea = 1024;
  threada_id = os_thread_create_arduino(thread1_task, NULL, priorityA, stack_sizea);

  // First thread is to do keep alive connectivity check (post requests every 5s)
  if (threada_id) {
    Serial.println("[THREAD] Keep-alive connectivity thread created successfully.");
  } else {
    Serial.println("[THREAD] Failed to create keep-alive connectivity thread.");
  }

  stack_sizeb = 2048;
  threadb_id = os_thread_create_arduino(thread2_task, NULL, priorityA, stack_sizeb);

  // Second thread is to get the signal to start THREAD process.
  if (threadb_id) {
    Serial.println("[THREAD] Start THREAD process thread created successfully.");
  } else {
    Serial.println("[THREAD] Failed to create Start THREAD process thread.");
  }
}







void setup()
{
    Serial.begin(115200);
        delay(2000);
    // Set up the threads
    thread.start_THREAD_threads();
}
void loop()
{
    // Empty or add non-blocking code here
}



so it can be done you just have to make sure to call the WiFiClient client inside the thread so it doesnt crash.

Hi @geofrancis,

Yes, your approach works! The multithreading framework you extracted from the OTA example has a clean structure, and using os_thread_create_arduino() to create independent threads is the correct method.

Here’s some feedback on your code:

1. Architecture Looks Good

Your THREAD class is well encapsulated:

  • start_THREAD_threads() handles thread creation
    • thread1_task and thread2_task serve as independent task functions
      • The main loop() stays empty or contains non-blocking code
    • This design will indeed allow GEN AI inference to run in the background without blocking the main loop.
  • 2. Consider Increasing Stack Size

Your current settings:

cpp
stack_sizea = 1024;
stack_sizeb = 2048;
```

For GEN AI examples, I'd suggest increasing these:
```cpp
stack_sizea = 4096;  // or higher
stack_sizeb = 4096;
```

AI inference typically uses more local variables and buffers.

**3. About the WiFiClient Issue You Found**

You mentioned that WiFiClient must be called inside the thread to avoid crashes - that's correct. The reason is that WiFiClient's internal socket resources are bound to the thread context where it was created; using it across threads causes issues.

**4. For Integrating GEN AI**

You can put the AI inference inside `thread1_task`:

```cpp
void THREAD::thread1_task(const void *argument) {
    // Initialize AI resources (inside the thread)
    
    while (1) {
        // Run AI inference
        // Process results
        delay(100);  // Appropriate interval
    }
}
```

This way your main loop stays responsive for other tasks (display, button input, etc.).

Overall, you're on the right track - this framework will work!

yes i had to increase the stack size on the first loop to stop it crashing when I added my code to it.

I dont understand why all the examples don’t use threading, the AI functions are essentially useless since you can’t do anything else while its running any AI or LLM examples without threading.