CPU Execution while handling multiple threads iMX7d

Hi,
Need expert suggestions on the below:
I have around 20 threads running all, almost all the threads have while(1),
Question is, how should I handle code in while(1) to optimize CPU execution, so far I do not have any timing related issues, but it’s just my concern as the code/threads grow up…

while(1)
{
if(flag) {
do_something();
}
}

You can use a

sched_yield(void);

http://man7.org/linux/man-pages/man2/sched_yield.2.html

Hi @alex.tx ,
thank you for reply and seems it will work for me.

Can I call sched_yield() from each thread just after my work/flags is done?

Thanks
Neeraj

Yes, you should an invoke sched_yield() as soon as you thread has nothing else to do.