Page 1 of 1

More small threads or few bigger ones ?

Posted: Tue Jun 02, 2020 5:10 pm
by AmperCZ
What is recommended use of threads (STM32F407) ?

In robot, I have one IMU thread reading data from two 6050 over I2C on Prio+2. There is about 3-4ms delay between reads (about 200Hz update from DMP on 6050s)

But now considering how to deal with other stuff - pwm stuff, some calculations, adc sensing, UART read/write. Right now I have about 5-6 additional threads handling this pieces of code with Prio+1 or NormalPrio on them and various sleep time (10s for RC stuff, 50 for rest).

Debug Thread window shows me that I have still plenty of Idle time so it should be ok from performance perspective but I would like to know how to consider one bigger thread with bigger stack space vs more smaller ones. Sure, more threads will consume more memory and I will loose more time in switching but code is much easier to read and maintain.

Is there some reason why to put all code from 5-6 threads (beside of mem/switch time) into one or this number of threads is ok for Chibios and I can enjoy them ?

Re: More small threads or few bigger ones ?

Posted: Tue Jun 02, 2020 5:31 pm
by Giovanni
Hi,

Except for memory there are no problems in splitting in multiple threads, RT is very efficient at that. Ask yourself if something has an advantage if done as a thread. For example, I saw somebody starting a thread then waiting for completion in order to get a result, that is simply a function call...

Giovanni

Re: More small threads or few bigger ones ?

Posted: Tue Jun 02, 2020 7:24 pm
by AmperCZ
thanks, I will keep it as it is because it is much easier to read and manage

Re: More small threads or few bigger ones ?

Posted: Wed Jun 03, 2020 9:36 am
by mikeprotts
If it's 'always' running processes, then dedicated threads makes sense. Thread priorities are useful to ensure that the most important tasks run first (after interrupts of course).

I try to avoid overloading interrupts with processing, so use threads to handle the larger processing times. Interrupts handle the timing sensitive code to minimise jitter, and set up the data for threads to process. Normal priority interrupts wake their relevant threads, the highest priority interrupts just set a few flags and global variables.

I do have a main thread that performs several tasks, but those are lower priority.

Mike