Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 950615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:35:44+00:00 2026-05-15T23:35:44+00:00

I’m trying to write a buffermanager that manages 3 Streams. The typical usage would

  • 0

I’m trying to write a buffermanager that manages 3 Streams. The typical usage would be with a slow producer and a fast consumer. The idea behind the three buffers is that the producer ALWAYS has a buffer to write in and the consumer ALWAYS gets the latest data produced.

Now i already have this, and it sort-off works.

namespace YariIfStream
{

    /// <summary>
    /// A class that manages three buffers used for IF data streams
    /// </summary>
    public class YariIFStream
    {
        private Stream writebuf; ///<value>The stream used for writing</value>
        private Stream readbuf; ///<value>The stream used for reading</value>
        private Stream swapbuf; ///<value>The stream used for swapping</value>
        private bool firsttime; ///<value>Boolean used for checking if it is the first time a writebuffers is asked</value>
        private Object sync; ///<value>Object used for syncing</value>

        /// <summary>
        /// Initializes a new instance of the Yari.YariIFStream class with expandable buffers
        /// </summary>
        public YariIFStream()
        {
            sync = new Object();
            eerste = true;

            writebuf = new MemoryStream();
            readbuf = new MemoryStream();
            swapbuf = new MemoryStream();
        }

        /// <summary>
        /// Returns the stream with the buffer with new data ready to be read
        /// </summary>
        /// <returns>Stream</returns>
        public Stream GetReadBuffer()
        {
            lock (sync)
            {
                Monitor.Wait(sync);
                Stream tempbuf = swapbuf;
                swapbuf = readbuf;
                readbuf = tempbuf;
            }
            return readbuf;
        }

        /// <summary>
        /// Returns the stream with the buffer ready to be written with data
        /// </summary>
        /// <returns>Stream</returns>
        public Stream GetWriteBuffer()
        {
            lock (sync)
            {
                Stream tempbuf = swapbuf;
                swapbuf = writebuf;
                writebuf = tempbuf;
                if (!firsttime)
                {
                    Monitor.Pulse(sync);
                }
                else
                {
                    firsttime = false;

                }
            }
            //Thread.Sleep(1);
            return writebuf;
        }

    }
}

The firsttime check is used because the first time a writebuffer is asked, it can not pulse the consumer because the buffer still has to be written with data. When a writebuffer is asked a second time, we can be sure the previous buffer contains data.

I have two threads, one producer and one consumer.
This is my output:

prod: uv_hjd`alv   cons: N/<]g[)8fV
prod: N/<]g[)8fV   cons: 5Ud*tJ-Qkv
prod: 5Ud*tJ-Qkv   cons: 4Lx&Z7qqjA
prod: 4Lx&Z7qqjA   cons: kjUuVyCa.B
prod: kjUuVyCa.B

Now it’s ok the consumer lags one behind, it is supposed to do that.
As you can see i lose my first string of data wich is my main problem.

The other problems are this:

  • if i remove the firsttime check, it works. But it shouldn’t in my opinion…
  • if i add a Thread.Sleep(1); in the GetWriteBuffer() it also works. Something i don’t understand.

Thanks in advance for any enlightenment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:35:45+00:00Added an answer on May 15, 2026 at 11:35 pm

    I’ve fixed my problem. I replaced all the Stream instances with byte[]. Now it works fine.
    Don’t know why Stream would not work, do not want to spent more time figuring this out.

    Here’s the new code for anyone running into the same problem.

    /// <summary>
    /// This namespace provides a crossthread-, concurrentproof buffer manager. 
    /// </summary>
    namespace YariIfStream
    {
    
        /// <summary>
        /// A class that manages three buffers used for IF data streams
        /// </summary>
        public class YariIFStream
        {
            private byte[] writebuf; ///<value>The buffer used for writing</value>
            private byte[] readbuf; ///<value>The buffer used for reading</value>
            private byte[] swapbuf; ///<value>The buffer used for swapping</value>
            private bool firsttime; ///<value>Boolean used for checking if it is the first time a writebuffers is asked</value>
            private Object sync; ///<value>Object used for syncing</value>
    
            /// <summary>
            /// Initializes a new instance of the Yari.YariIFStream class with expandable buffers with a initial capacity as specified
            /// </summary>
            /// <param name="capacity">Initial capacity of the buffers</param>
            public YariIFStream(int capacity)
            {
                sync = new Object();
                firsttime = true;
    
                writebuf = new byte[capacity];
                readbuf = new byte[capacity];
                swapbuf = new byte[capacity];
            }
    
            /// <summary>
            /// Returns the buffer with new data ready to be read
            /// </summary>
            /// <returns>byte[]</returns>
            public byte[] GetReadBuffer()
            {
                byte[] tempbuf;
                lock (sync)
                {
                    Monitor.Wait(sync);
                    tempbuf = swapbuf;
                    swapbuf = readbuf;
                }
                readbuf = tempbuf;
    
                return readbuf;
            }
    
            /// <summary>
            /// Returns the buffer ready to be written with data
            /// </summary>
            /// <returns>byte[]</returns>
            public byte[] GetWriteBuffer()
            {
                byte[] tempbuf;
                lock (sync)
                {
                    tempbuf = swapbuf;
                    swapbuf = writebuf;
    
                    writebuf = tempbuf;
    
                    if (!firsttime)
                    {
                        Monitor.Pulse(sync);
                    }
                    else
                    {
                        firsttime = false;
                    }
                }
                return writebuf;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 486k
  • Answers 486k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try adding the -webkit-user-select: none; declaration to each of the… May 16, 2026 at 8:06 am
  • Editorial Team
    Editorial Team added an answer There is no point in cloning a long. You should… May 16, 2026 at 8:06 am
  • Editorial Team
    Editorial Team added an answer I devoted an entire class to Quartz 2D drawing last… May 16, 2026 at 8:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.