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?

share|improve this question
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 '15 at 6:16
up vote 4 down vote accepted

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.

Cheers!

share|improve this answer

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.

share|improve this answer
    
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 '15 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 '15 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 '15 at 15:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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