Multi-threading in Arduino?

Does anyone know how to do multithreading in arduino, or if its even possible?

Thanks in advance! :slight_smile:

Yes itโ€™s possible. There are several scheduler and RTOS libraries around. I donโ€™t know where though, have you tried searching on the forum and Googling?


Rob

It's certainly possible, but I'd question whether the overhead of multi-threading is sensible on a processor with such limited resources, and whether a project which was complicated enough to justify that overhead would be suitable to run on an Arduino anyway. I suspect that the reason for wanting multithreading is that you want to do several things concurrently and are used to designing blocking code; the more sensible approach in that case is to learn how to design non-blocking code so that you can do all those things in a single thread.

If you must have threading, more horsepower seems desirable. I'd check out pcDuino, Netduino, Raspberry Pi, and similar processors.

I am wanting to build a quadcopter and want separate tasks that will continually check the gyro sensor (for self balance), one to receive commands from the controller, and one to do directional computing with the GPS.
Whats "blocking code"? I'm not familiar with that term.

Oh and I'm doing this on an uno if the changes anything.

PeterH:
...I suspect that the reason for wanting multithreading is that you want to do several things concurrently and are used to designing blocking code; the more sensible approach in that case is to learn how to design non-blocking code so that you can do all those things in a single thread.

If one processor is no enough, then several boards communicating via spi (master-slaves) might be an option.

Whats "blocking code"?

Code that prevents anything else happening. The classic example on the Arduino is the delay() function. Suppose you wanted to check your GPS every second and had delay(1000);in the code between checks. You could do nothing else during the 1 second delay, such as checking and responding to gyros.

There are techniques available to get round this such as checking every millisecond or microsecond whether it is time to do something, such as checking the GPS, and in the meantime do something else such as checking the gyros.

Whats "blocking code"? I'm not familiar with that term.

Code (usually a function) that does not return until it's finished it's job, no matter how long that takes. It's the easy way to code but not great if you have a lot of other things to do.


Rob

The demo code I posted in this thread illustrates how several things can be managed at the same time without multi-threading.

...R