C/C++ Message Passing


Note: For C++ users, find an update at https://blog.jdboyd.net/2010/11/small-revisit-of-cc-message-passing-in-a-threaded-program/

My favorite approach to threaded programming is message passing, holding the shared memory for things that need it for performance reasons.

eCos has a handy general purpose message passing system, but C/C++ do not. Posix does have some methods that can be used for this. There are pipes or System V message queues. My problem with these is that they are meant for interprocess work, and thus have un-needed overhead for talking between threads. Also, those two methods are byte stream oriented.

What eCos has and what I wanted was the ability to pass pointers to a messages struct. Basically, a thread safe queue that pushes void* in and pops void* out, without invoking syscalls, which sap performance. I typically use this with a struct. Either all of the structs in the queue are the same type, or else in every struct used, the first item in the struct is an int indicating what struct to coerce the pointer to.

Anyway, here are the files:

Tested on Linux with GCC 3.2 and Solaris with Sun C++ 5.5. On Solaris, the test program for the queue returns two warnings, but functions fine. The test program is C++ for reasons that aren’t really relevant. This should be fixed at some point.

This is meant to be written to be acceptable for use in realtime systems. Obviously, if those systems don’t support posix threads, one will need to substitute the OS’s form of Mutex’s and Conds.


Leave a Reply