More small threads or few bigger ones ?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
AmperCZ
Posts: 15
Joined: Fri Jan 25, 2019 11:46 pm
Been thanked: 1 time

More small threads or few bigger ones ?

Post 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 ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: More small threads or few bigger ones ?

Post 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
AmperCZ
Posts: 15
Joined: Fri Jan 25, 2019 11:46 pm
Been thanked: 1 time

Re: More small threads or few bigger ones ?

Post by AmperCZ »

thanks, I will keep it as it is because it is much easier to read and manage
mikeprotts
Posts: 166
Joined: Wed Jan 09, 2019 12:37 pm
Has thanked: 19 times
Been thanked: 31 times

Re: More small threads or few bigger ones ?

Post 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
Post Reply