CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: Questions about a sample code on shared memory.

    Yeah, I should do some testing too.

  2. #17
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Questions about a sample code on shared memory.

    Quote Originally Posted by vincegata View Post
    @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.

  3. #18
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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?

  4. #19
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    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.

  5. #20
    Join Date
    Nov 2003
    Posts
    1,902

    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

  6. #21
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    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.

  7. #22
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    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.

Page 2 of 2 FirstFirst 12

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
  •  





Click Here to Expand Forum to Full Width

Featured