Threading in C++

Hi,

I am developing a UAV control system on the Beaglebone Black. I have tried to use threads (infinate loops, so they run continuously) in my application to send telemetry data from the plane, receive instructions from the ground, control the plane and so on. I found that the threads run fine most of the time, but once in a while there is a snag that occurs, which can last up to 3 secs. This is very catastrophic for my orientation algorithm as because it integrates gyro data over the small time scales it usually takes per iteration.

I have found that when I run the code serially, the snags disappear completely, but they return even when I introduce a thread that does nothing (infinite loop with a sleep). This means that it has nothing to do with threads accessing the same data simultaneously. It appears to be a consequence of merely having threads.

I have used boost::threads as well as std::threads but they both have the snag once every few seconds.

  • The board I am using is the beaglebone black rev c
  • I am running ubuntu 14.04
  • The kernel is 3.8.13-bone68

I would really appreciate any help because I really need to be able to run threads.

Kind regards,
Lesiba Mahapa

Threads are serialized by the OS, especially if there are more threads than cores. If you now have a look at how many threads/processes are running on your machine (e.g. by using "ps aux"), you might get an idea how complicated that topic actually is, especially on a single core system.

To give a small hint:

Whenever your code enters a system call the OS might decide to give up the remaining piece of the time slice reserved for the actual thread.

So it might be a good idea to enable preemption in the kernel, adjust the timer frequency there and have a look at all the other knobs a Linux system offers (e.g. sysctl -a), besides being aware of locking problems and such in your own code. Not to mention problems with slow I/O (e.g. swap on sd-cards).

Regards,

Alexander Holler

Hi,

Hi,

I am developing a UAV control system on the Beaglebone Black. I have
tried to use threads (infinate loops, so they run continuously) in my
application to send telemetry data from the plane, receive instructions
from the ground, control the plane and so on. I found that the threads
run fine most of the time, but once in a while there is a snag that
occurs, which can last up to 3 secs. This is very catastrophic for my
orientation algorithm as because it integrates gyro data over the small
time scales it usually takes per iteration.

You might be interested in http://dev.ardupilot.com/wiki/building-the-code/building-for-beaglebone-black-on-linux/

Cheers,
Jan.