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

  • SEARCH
  • Home
  • 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 6551113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:17:48+00:00 2026-05-25T12:17:48+00:00

Properties of the bounded buffer class I’m trying to build… Multiple producer, multiple consumer.

  • 0

Properties of the bounded buffer class I’m trying to build…

  • Multiple producer, multiple consumer.
  • Blocking producer and blocking consumer.
  • Uses AtomicInteger as the read/write pointers.
  • Uses AtomicReferenceArray (taking in a generic type) for holding the buffer.
  • Buffer is of Short.MAX_VALUE size and it uses CAS to deal with overflows.

Now for the problem…

Problem: I can’t seem to comment out the synchronized(this) blocks in the code below. The whole point of using AtomicInteger as pointers I thought was to avoid doing this.

Commenting out the synchronized(this) blocks yields in consumers missing some items that producers have put in. If I include the synchronized(this) block, everything’s great and every single thing produced is consumed.

What am I missing?

public class BoundedBuffer<T> {
    private static final int BUFFER_SIZE = Short.MAX_VALUE+1;
    private AtomicReferenceArray<T> m_buffer = null;
    private Semaphore m_full = new Semaphore(BUFFER_SIZE);
    private Semaphore m_empty = new Semaphore(0);
    private AtomicInteger m_writePointer = new AtomicInteger();
    private AtomicInteger m_readPointer = new AtomicInteger();

    public BoundedBuffer() {
        m_buffer = new AtomicReferenceArray<T>(BUFFER_SIZE);
    }

    public static int safeGetAndIncrement(AtomicInteger i) {
        int oldValue = 0, newValue = 0;
        do {
            oldValue = i.get();
            newValue = (oldValue == Short.MAX_VALUE) ? 0 : (oldValue + 1);
        } while (!i.compareAndSet(oldValue, newValue));
        return oldValue;
    }

    public void add(T data) throws InterruptedException {
        m_full.acquire();
        synchronized (this) { // << Commenting this doesn't work
            // CAS-based overflow handling
            m_buffer.set(safeGetAndIncrement(m_writePointer),data);
        }
        m_empty.release();
    }

    public T get() throws InterruptedException {
        T data = null;
        m_empty.acquire();
        synchronized (this) { // << Commenting this doesn't work
            // CAS-based overflow handling
            data = m_buffer.get(safeGetAndIncrement(m_readPointer));
        }
        m_full.release();
        return data;
    }
}
  • 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-25T12:17:48+00:00Added an answer on May 25, 2026 at 12:17 pm

    There could be a problem where the get() from the array is not atomic with the increment when the synchronized block is removed. The breaking scenario I speculate on requires the producer to be overrunning the consumers, then you could have the producer overwrite an array entry that has not been read yet IF the semaphore release was triggered by an out-of-order read.

    Consider the situation where the buffer is full (writer index is at N, reader index is at N+1) and 2 threads are trying to read from the buffer. (Assume that N is not close to the wraparound point for simplicity.)

    Thread 1 receives the index N+1 from which to read its item.

    Thread 2 receives the index N+2 from which to read its item.

    Due to a fluke of scheduling Thread 2 gets from the buffer array first and releases the m_full semaphore before Thread 1 gets its item from the array.

    Thread 3 (a producer) wakes up and writes an item into the next available slot N+1 in the buffer, also before Thread 1 has read from the buffer.

    Thread 1 then gets the item at index N+1, but has missed the item it wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It strikes me that Properties in C# should be use when trying to manipulate
properties/C/C++ Build/Settings GCC C++ Linker/Libraries Under libraries(-I) I have libbost_system libbost_filesystem ... and under
The properties of my figure I want to build are the following: The figure
My class needs two properties: startTime and endTime . What is the best class
I'm trying to use command objects in the following way: class BeneficiaryCommand { List<Beneficiary>
In .NET properties are supposed to be first class citizens however in the IL
Say I have an email class that has the following properties: public string From
Example.properties user=somePerson env=linux file=mpg properties.java class propertiestTest.java { Properties props = new Properties(); props.setProperty(user,
How are properties for a Collection set? I've created a class with a Collection
I know that the Properties class is a sub-class of Hashtable. So all the

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.