[c++] multiple programs

I want to know if there is any way to operate multiple C programs simultaneously.

I wrote C program with functions(BrickPiSetup(), BrickPiSetupSensors()).
And It worked well alone.
($ gcc -o program program.c -lrt -lm

$./program)

But when I operate two or more programs simultaneously,
previous program began to stop when the last program started to operate BrickPiSetupSensors().

Is it impossible to operate several programs simultaneously with BrickPi?

With Python I have had issues with that. Not sure what your trying to accomplish, but running one program with multiple threads may work.

IIRC, only 1 single program in the Linux user space may have access to the BP shields.
OTOH, this single program may have multiple threads (multithreading by pthread or C++(11) std::threads), and then you can run those threads simultaneously.
Nonetheless you must make sure that only 1 thread accesses the BP shields by write actions at a time, but that is no issue actually because you can have control over that by mutexes. Read actions are so quick that they wouldn’t interfere and don’t change shared values so they don’t have to be mutexed and may be called in either thread independently, but extra mutexes would do no harm for that purpose though.

I already showed how to do that here in this forum by the source code I once posted, using pthread threads and pthread mutexes.

If you’re using BrickPi+ you will certainly need mutexes for multithreading. If you’re using BrickPi3, you might not need mutexes.

Certainly it will be more complicated to have multiple processes.

pthread threads are actually very easy to handle, almost as easy as multithreading with NXC, but far more powerful than that (both pre-emptive nonetheless), and eventually multithreading is indispensible for robotics IMO. Once having understood the pthread schematics or patterns, it makes parallel threading even easier then complicated nesting of functions and jobs into 1 single thread!

1 Like