Yeah, I should do some testing too.
Printable View
Yeah, I should do some testing too.
You would only need shared memory if the 2 threads are in different processes.
If two threads are in the same process, then they share the same process space and thus share all the same memory anyway.
You only need synchronisation between 2 threads in a single process, to make sure that any memory access is "safe".
There is an implementation of a lock-free single linked list in Windows (XP/2003 and up)
Have a look on MSDN for InitializeSListHead().
It can handle Multi-producer, multi consumer scenario's out of the box if you're ok with LIFO behaviour.
if you need FIFO behaviour, then Multi-consumer becomes complex.
FIFO on a single consumer is easy. pop the entire list, reverse the list (which you can do by making a 2Nd SLIST), and process any items in the popped list before popping more items from the main list.
BTW. While polling may appear faster. It also has the nasty habit of consuming 100% CPU power, this in and off itself can be a big issue if this needs to run 24/7 on a server.
Polling loops are:
- A bad habit
- Often introduced by a lack of understanding of what is really going on
- Often assumed to be faster, only because a poor synchronisation methodology
- egotistical CPU eaters.
In many cases "fast" is less of a concern than "behaves properly". Are you prepared to accept "faster" if it means all the rest of the applications/services on your PC run less efficient?
@OReubens - I am considering of merging my producer and consumer into the same process instead of using shared memory hence was my question about locked-free linked list. I cannot use InitializeSListHead() since I am on Linux, but I've found some articles googling. I do need FIFO. Polling is not good, I agree.
Forget about lock-free anything. That would be your absolute last resort, with tons of profile data to back that decision up.
Think in terms of "reduce lock contention". Here is an example that uses both the double-buffer and ring-buffer techniques to reduce lock contention: http://forums.codeguru.com/showthrea...81#post2057581
Hah - I went looking for that code only to find it was your thread :)
gg
@Codeplug - ah, you helped me with multithreading too and now it came to the full circle... :)
I do use that code: it is now my consumer process where "producer" thread receives the data over FIFO from the producer process.
Thank you for all the help I received, I've decided to merge the processes to use observer pattern instead of using shared memory.