Thread Safety/Reentrancy in WPILib

Hi all,

I was wondering if anyone knew if the the motor output functions in WPILib are thread safe. If they aren’t, worst case, I wil just use a binary semaphore to ensure that only one thread is using the motors at the same time.

Thanks.

As far as I know, WPILib speed controller objects are not thread-safe.

I’m curious; why would you want to set the output of a speed controller from more than one thread?

It’s not that I want to set the speed of the same motor from different threads, but that I want to set the speeds of different motors from different threads. However, depending on how the cRIO communicates with the digital sidecar, it might pose a problem. For example, if it uses a serial command format, and, let’s say we want to turn on two motors at the same time from different threads. Usually, it would send the open command followed by the motor ID. However, if we were unlucky, it would send the open command to turn on the first motor, and then it would switch threads to the other motor thread. That thread would then send its open command, which would be interpreted by the digital sidecar as a motor ID, causing sporadic motor movement.

Oh, I see what you mean. In that case, yes, the lower-level implementation is thread-safe.

WPILib has no problem updating unique speed controller objects from within different threads.