I have a pthreads app that I currently use regular linux pipes to communicate and send messages to the various threads. Its works fine and is not really broken but I have a nagging feeling that it can be improved.
If I want to design for the least latency, what is the fastest method to communicate between pthreads? Would using condition variables be faster than pipes?
I need to pass small amounts of data, as a single int optcode with a few parameters. Will pthreads condition variables let me pass data between threads?
Thanks,
-Andres
Since it’s between threads then either shared memory or a lock free queue would be quite a bit faster.
The choice between which to choose will depend on the data you’re passing around. A static buffer shared between two threads guarded by a lock is fine. Where you are generating data and passing it to another thread perhaps a queue is a better choice.
Some implementations of bounded lockfree MPMC queues are available here: http://www.1024cores.net
By the way, be careful writing your own lock free data structures. They are not as trivial as you might expect to get right. A locking version will be a good start and will still out-perform pipes.
Between processes linux pipes as you call them (or unix domain sockets are a good option).