-
October 17th, 2013, 07:52 PM
#16
Re: Questions about a sample code on shared memory.
Yeah, I should do some testing too.
-
October 18th, 2013, 07:39 AM
#17
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.
-
October 18th, 2013, 07:44 AM
#18
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?
-
October 18th, 2013, 01:37 PM
#19
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.
-
October 18th, 2013, 04:35 PM
#20
Re: Questions about a sample code on shared memory.
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
-
October 18th, 2013, 09:16 PM
#21
Re: Questions about a sample code on shared memory.
@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.
-
October 24th, 2013, 09:36 PM
#22
Re: Questions about a sample code on shared memory.
Thank you for all the help I received, I've decided to merge the processes to use observer pattern instead of using shared memory.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|