7

Well I've just started learning the concept of multithreading with C++ and immediately a bunch of questions came to mind about the possibility of using multithreading with microcontrollers in general and Arduino specifically.

So, is it possible to use multithreading on any type of Arduino boards?

Greenonline
  • 3,152
  • 7
  • 36
  • 48
Omar Hussein
  • 101
  • 1
  • 7

4 Answers4

7

In Cosa you can find the following support for concurrent programming:

  1. Events, interface interrupt service routines
  2. Jobs, delayed, periodic or alarm functions with us, ms and seconds level timers (Watchdog, RTT or RTC).
  3. FSM, object-state function
  4. ProtoThreads, object-state pointer
  5. Threads, Semaphores, etc, multiple stacks
  6. UML Capsules and Connectors, dependency driven programming

There are plenty examples on how to use these. A good starting point is the Blink sketches. There is even a multi-threading Blink example with a thread that does the LED on/off and a controller thread that periodically changes the blink period. The thread stack size is only 64 bytes and it runs even on an ATtiny.

With all the AVR internal hardware modules (such as SPI, TWI, UART, etc) there is plenty of opportunities for concurrency.

A Scheduler library for the Arduino core is also available. This is a port of the Cosa Threads. Please see github for further details.

Cheers!

Mikael Patel
  • 7,989
  • 2
  • 16
  • 21
3

The really quick answer is "maybe" – it depends on what you mean by "Arduino" and what you mean by "thread." The answer is likely to be different for the AVR based Arduinos (Uno et al.) vs. the ARM based Arduinos (Due et al.) – I would expect there to be much better hardware support for "real" threads on the ARM processors. Another question you'd want to answer is "why threads?" Do you want the abstraction to help you organize your code? Or do you actually need "real" threads?

Before there was hardware thread support (e.g., the mid-80s) there are user thread implementations, it seems possible that they might be adaptable to run even on an AVR. I would expect it to be something of a project.

There is a threads package called Protothreads which may be interesting. The description says "Protothreads are extremely lightweight stalkless threads designed for severely memory constrained systems." I found another question asking about simple usage of Protothreads, so it seems that you may find some other users of the package.

You may also find some useful information in this Stack Exchange question on threads, a quick search for "C user threads" found this implementation on the first page – and I'm sure there are many more.

A search on "Arduino threads" found several more interesting looking links:

If you just want threads, a small and inexpensive board, and I/O pins it might be worth considering a Raspberry Pi – Linux has thread support.

dlu
  • 1,661
  • 2
  • 15
  • 29
3

There's one Arduino-like product that certainly could enable multi-threading, as it is multi-core: the Shield Buddy TC275.

Core setup and loop functions

So basically you have three setup() functions, and three loop() functions. True multi-threading.

dda
  • 1,595
  • 1
  • 12
  • 17
1

I use Atomthreads on Atmega128, it is very lightweight with minimum overhead. Have task scheduler, mutex, semaphores and queue. Code is portable but might require some configuration to use with Arduino IDE (I use Atmel Studio). I primarily use task scheduler, never had problems. Just checked, the development is still active.

Flanker
  • 529
  • 2
  • 8