I am using a quad-core embedded computer with Linux as OS for controlling robot system.
Basically the project is multi-threaded, single-process program written in C++.
Here are some of the backgrounds and requirements;
- Some task (signal processing and communication with hardware) requires some what strict "real-time" operation.
- Cycle period is 500us or 1000us (configurable)
- The counter party of the operation is so-called 'hard real-time HW' (one with designated DSP)
- Missing 1~2 cycle occasionally (may be due to jitter) will cause hardly noticeable degradation of system operation.
- Missing 3~9 cycle occasionally will cause quite noticeable degradation of system performance but not fatal.
- Missing >10 cycle at least once will cause whole system stop and considered fatal major malfunction.
The solution in my mind is combination of below.
- Put all the function required for 'real-time operation' in a single thread and make it 'real-time thread'.
- Put other functions in one or two threads and make it 'non-real-time threads'.
- Set
sched_priorityof the real-time thread around 95. - Spare a CPU core for real-time operation by manipulating the CPU-affinity of all the default linux services to use 3 cores except 'real-time core' and set thread CPU affinity of the 'real-time thread' to use spared real-time core (so that critical tasks get minimum interference).
- Inter-thread communication will be done with
std::atomicvariables with memory orderreleaseandacquirebut minimized.
Would it be a good practice for the application? Is there any other practice for more stable operation?