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?

2
  • 2
    By multithreaded, do you also mean concurrent? If you don't, Interrupt routines triggered by a timer at even intervals can cover a lot of similar functionality on AVR processors.
    – BrettAM
    Dec 9, 2015 at 6:16
  • You could try this library github.com/mikaelpatel/Arduino-Scheduler Jan 16, 2017 at 10:03

4 Answers 4

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!

2
  • Of what order is the memory overhead of Cosa? 100 KB flash and 20 KB RAM? Feb 15, 2018 at 22:30
  • Depends on what you use/include. In general the "overhead" is less than the Arduino core for similar functionality. Best way to find out is to run some of the example sketches. Feb 16, 2018 at 10:41
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.

3
  • 1
    Good answer. It also might be worth noting that even with hardware thread support, an OS or at least some software will layer will probably need to manage and schedule the threads.
    – Mlagma
    Dec 9, 2015 at 3:29
  • @Mlagma: As far as I can tell, each link in this answer shows an implementation of threads that are statically compiled into the user's program. The Arduino runs only that one program. Can you give even one example of an OS or other software layer, other than the user program?
    – David Cary
    Dec 9, 2015 at 14:15
  • @Mlagma, it could be as simple as something like coroutines and interrupts. I'm not expecting to find much beyond maybe a library that implements a basic scheduler – but I haven't found even that.
    – dlu
    Dec 9, 2015 at 15:08
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.

4
  • 1
    +1 for finding this. Excellent board, but not cheap. However, it would give AeroQuad a new lease of life..! Jan 16, 2017 at 18:10
  • 1
    Yeah it's been on my radar for a while but the cost... I hope Chinese clone-makers find it soon :-)
    – dda
    Jan 17, 2017 at 4:56
  • 1
    It's not as compatible as I initially thought though, for reasons that are, now, obvious. A simple recompilation may not be sufficient. It will depend on the complexity of the code, i.e. if assembler has been used. See Has anyone used a Shield Buddy (en lieu of a Arduino Mega2560) as the RAMPS 1.4 host board? Jan 17, 2017 at 5:13
  • 1
    Yeah, ASM could be a problem. Plus, adjusting the code to a 3-core environment, even without ASM, would require some work. But I think for a new project, it could be indeed interesting.
    – dda
    Jan 17, 2017 at 5:27
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.

1
  • 1
    Re: "very lightweight". Can you quantify that? Is it 8 KB of flash and 600 bytes of memory? Feb 15, 2018 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.