If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Questions about a sample code on shared memory.

Originally Posted by vincegata

@OReubens - so I have a producer and a consumer, the point is to send the data as fast as possible. Since synchronization takes time, a person who developed the code that I posted, used polling instead of semaphores.

Thx about linked list, is it possible to have a lock-free linked list in a memory of a process instead of a shared memory? Can I use it to exchange data between threads in a lock-free manner?

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.

Re: Questions about a sample code on shared memory.

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?

Re: Questions about a sample code on shared memory.

@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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.