Tech Support > Computers & Technology > Programming > buffer implementation using semaphores
buffer implementation using semaphores
Posted by metrix007@yahoo.com on September 16th, 2005


I have a Semaphore and Buffer class, but I am not sure if I have
implemented it correctly, it is posted here now, I was just after tips
and suggestions..or pointing out erros which are not obvious(to me).
/*
This class implements a bounded buffer that stores Objects and
uses
blocking primitives based on semaphores when the buffer is full

or
empty. The buffer can be accessed by any number of threads.
*/
public class Buffer
{


// ...
// private Object store[] = new Object[size];
private int inptr = 0;
private int outptr = 0;
private int size = 0;
Semaphore spaces = new Semaphore(size);
Semaphore elements = new Semaphore(0);


// creates a bounded buffer with capacity cap
public Buffer()
{


}


// places the object o in the buffer; if no space is left in
// the buffer, block until a space becomes available
public void put(Object o)
{
spaces.down() ;
// store[inptr] = o ;
inptr = (inptr+1) % size ;
elements.up() ;
}


// take an item from the buffer and return it; if there is no
item in
// the buffer, block until one becomes available
public Object get()
{
Object value = null ;
elements.down() ;
// value = store[outptr] ;
outptr = (outptr+1) % size ;
spaces.up() ;
return value ;
}



}


and semaphore is pretty straighforward...

/*
This class implements a general semaphore. Since the semaphore



is blocked using Java condition synchronisation, no guarantee
can
be made about the order in which blocked threads are woken up.
Most of this code is based on the semaphore implementation
provided in Magee & Kramer (pg 91).
*/
public class Semaphore
{
protected int value; // value of the semaphore


// class constructor that initialises the value of the
semaphore
// to initial
public Semaphore(int initial)
{
value = initial;
}


// increment the value of the semaphore and wake up one waiting



// thread, if there is one
public synchronized void up()
{
++value;
notify();
}


// wait for the semaphore to have a positive value and then
// decrement the value by one
public synchronized void down()
{
// block thread until value of the semaphore is
positive
while(value == 0) {
try {
wait();
} catch (java.lang.InterruptedException e) {
// if call is interrupted, print the
current
// call stack, but do not exit the loop



e.printStackTrace(System.err);
}
}
// decrement value of the semaphore
--value;
}



}

Posted by blmblm@myrealbox.com on September 17th, 2005


In article <1126893313.581726.227450@g49g2000cwa.googlegroups .com>,
<metrix007@yahoo.com> wrote:
Replied to in comp.lang.java.programmer. I think this is a case in
which cross-posting would have been a good idea. Others may disagree,
I suppose.

[ snip ]

--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.

Posted by Joe Seigh on September 17th, 2005


metrix007@yahoo.com wrote:
This won't work for more than one producer. E.g.
thread 1: spaces.down();
thread 2: spaces.down();
thread 1: store[inptr] = o;
thread 2: store[inptr] = o; // overlays store by thread 1

You're going to lose objects and/or object stores. There
are other problems that can occur also. I only gave one
example. You could lose the increment of inptr. Etc, etc, ...

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.


Similar Posts