buffer implementation using semaphores

M

metrix007

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;
}
}

many thanks
larry
 
B

blmblm

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).

The only potential problem I notice in your code is that in Buffer,
you don't do anything to guarantee that only one process at a time
can access the buffer's variables (inptr and outptr -- this would
also apply to "store", except that it's commented out).

I notice that you've posted the same message in comp.programming.
This strikes me as a case in which crossposting would save everyone
a bit of time, with no ill effects. Others may disagree.
/*
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;
}
}

many thanks
larry
 
W

Wibble

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).


The only potential problem I notice in your code is that in Buffer,
you don't do anything to guarantee that only one process at a time
can access the buffer's variables (inptr and outptr -- this would
also apply to "store", except that it's commented out).

I notice that you've posted the same message in comp.programming.
This strikes me as a case in which crossposting would save everyone
a bit of time, with no ill effects. Others may disagree.

/*
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;
}
}

many thanks
larry
Java already has a class to do this:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html
 
M

metrix007

Hmm, here is my updated buffer class, semaphore class is the same but
here is also testbuffer class, which is meant to print out 1 to 10, but
instead prints out 0 to 90..as in 0, 10, 20..., 90

any ideas...its quite confusing :/

/*
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
{
int cap;
Object store[];
int inptr = 0;
int outptr = 0;
Semaphore spaces;
Semaphore elements;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
this.cap = cap;
store = new Object[cap];
spaces = new Semaphore(cap);
elements = new Semaphore(0);
}

// 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) % cap ;
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) % cap ;
spaces.up() ;
return value ;
}
}

and the testbuffer

// Test driver with a single test case for testing Buffer.java

// Simple producer thread that puts 10 items into the buffer
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 1; i < 11; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
p.start();
c.start();
}
}





[ snip ]

So it does. I'm mildly curious about whether they do something more
or less like the OP's code -- not quite curious enough to track down
the source code, but mildly curious.

Writing one's own version is still useful as a learning experience.
 
W

Wibble

Hmm, here is my updated buffer class, semaphore class is the same but
here is also testbuffer class, which is meant to print out 1 to 10, but
instead prints out 0 to 90..as in 0, 10, 20..., 90

any ideas...its quite confusing :/

/*
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
{
int cap;
Object store[];
int inptr = 0;
int outptr = 0;
Semaphore spaces;
Semaphore elements;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
this.cap = cap;
store = new Object[cap];
spaces = new Semaphore(cap);
elements = new Semaphore(0);
}

// 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) % cap ;
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) % cap ;
spaces.up() ;
return value ;
}
}

and the testbuffer

// Test driver with a single test case for testing Buffer.java

// Simple producer thread that puts 10 items into the buffer
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 1; i < 11; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
p.start();
c.start();
}
}





Wibble said:
(e-mail address removed) wrote:

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).
[ snip ]


So it does. I'm mildly curious about whether they do something more
or less like the OP's code -- not quite curious enough to track down
the source code, but mildly curious.

Writing one's own version is still useful as a learning experience.
You're putting b.put(new Integer(10*i)) into the buffer. Thats why its
multiplied by 10.
 
B

blmblm

Hmm, here is my updated buffer class, semaphore class is the same but
here is also testbuffer class, which is meant to print out 1 to 10, but
instead prints out 0 to 90..as in 0, 10, 20..., 90

any ideas...its quite confusing :/

Someone else has answered your question about why 0, 10, etc. (those
are the values you stored).

I will say that you didn't correct the problem I mentioned in a
previous post (I think in this newsgroup, might have been in another
one, since you multiposted your question):

You need to make sure only one thread at a time modifies variables
inptr, output, and store. Your code doesn't do that. The classic
method of doing so involves one more semaphore. On the off chance
that this is homework, or even something you're doing on your own
to learn, I won't say more now. Ask again if you can't figure it
out, or if whatever reference/book you're using doesn't say.
/*
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
{
int cap;
Object store[];
int inptr = 0;
int outptr = 0;
Semaphore spaces;
Semaphore elements;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
this.cap = cap;
store = new Object[cap];
spaces = new Semaphore(cap);
elements = new Semaphore(0);
}

// 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) % cap ;
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) % cap ;
spaces.up() ;
return value ;
}
}

and the testbuffer

// Test driver with a single test case for testing Buffer.java

// Simple producer thread that puts 10 items into the buffer
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 1; i < 11; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
p.start();
c.start();
}
}





(e-mail address removed) wrote:
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).

[ snip ]
Java already has a class to do this:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html

So it does. I'm mildly curious about whether they do something more
or less like the OP's code -- not quite curious enough to track down
the source code, but mildly curious.

Writing one's own version is still useful as a learning experience.
 
M

metrix007

its not homework per se, i am trying to study this on my own time and
dime, it an exercise from the book i purchased.., but it doesnt say
anything about ensuring it works fine with more than one thread...

Id be very interested in some more details on this.., as I was not sure
about using an extra semaphore...

Cheers


Hmm, here is my updated buffer class, semaphore class is the same but
here is also testbuffer class, which is meant to print out 1 to 10, but
instead prints out 0 to 90..as in 0, 10, 20..., 90

any ideas...its quite confusing :/

Someone else has answered your question about why 0, 10, etc. (those
are the values you stored).

I will say that you didn't correct the problem I mentioned in a
previous post (I think in this newsgroup, might have been in another
one, since you multiposted your question):

You need to make sure only one thread at a time modifies variables
inptr, output, and store. Your code doesn't do that. The classic
method of doing so involves one more semaphore. On the off chance
that this is homework, or even something you're doing on your own
to learn, I won't say more now. Ask again if you can't figure it
out, or if whatever reference/book you're using doesn't say.
/*
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
{
int cap;
Object store[];
int inptr = 0;
int outptr = 0;
Semaphore spaces;
Semaphore elements;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
this.cap = cap;
store = new Object[cap];
spaces = new Semaphore(cap);
elements = new Semaphore(0);
}

// 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) % cap ;
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) % cap ;
spaces.up() ;
return value ;
}
}

and the testbuffer

// Test driver with a single test case for testing Buffer.java

// Simple producer thread that puts 10 items into the buffer
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 1; i < 11; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
p.start();
c.start();
}
}





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).


[ snip ]

Java already has a class to do this:


http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html

So it does. I'm mildly curious about whether they do something more
or less like the OP's code -- not quite curious enough to track down
the source code, but mildly curious.

Writing one's own version is still useful as a learning experience.
 
B

blmblm

its not homework per se, i am trying to study this on my own time and
dime, it an exercise from the book i purchased.., but it doesnt say
anything about ensuring it works fine with more than one thread...

If you only have one producer and one consumer, your current solution
is probably okay. However, if you could have more than one of each or
either, there could be problems. I think someone else posted a scenario
in which multiple threads changing inptr at the same time could cause
problems; same thing for outptr. Did that make sense to you?
Id be very interested in some more details on this.., as I was not sure
about using an extra semaphore...

The classic semaphore-based solution to the bounded buffer problem
uses three semaphores -- the ones you have, plus another to guarantee
one-at-a-time access to the buffer. It should have initial value 1,
and then every place in the code that accesses the buffer's variables
(inptr, outptr, etc.) should be preceded by a "down" on this semaphore
and followed by an "up". There are other ways of doing this in
Java, of course, but that's the classic semaphore-based approach.
(If your book talks about "the mutual exclusion problem", that's what
I'm talking about here.)

If you're specifically interested in learning how this stuff works
in Java (as opposed to in general), it would probably be interesting
for you to think about how to fix the problem I'm talking about
(ensuring one-at-a-time access) using Java synchronized blocks/methods.

(By the way -- why do you say you weren't sure about using an extra
semaphore? you didn't think it was needed, or thought it might cause
other problems, or what?)
Cheers


Hmm, here is my updated buffer class, semaphore class is the same but
here is also testbuffer class, which is meant to print out 1 to 10, but
instead prints out 0 to 90..as in 0, 10, 20..., 90

any ideas...its quite confusing :/

Someone else has answered your question about why 0, 10, etc. (those
are the values you stored).

I will say that you didn't correct the problem I mentioned in a
previous post (I think in this newsgroup, might have been in another
one, since you multiposted your question):

You need to make sure only one thread at a time modifies variables
inptr, output, and store. Your code doesn't do that. The classic
method of doing so involves one more semaphore. On the off chance
that this is homework, or even something you're doing on your own
to learn, I won't say more now. Ask again if you can't figure it
out, or if whatever reference/book you're using doesn't say.
/*
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
{
int cap;
Object store[];
int inptr = 0;
int outptr = 0;
Semaphore spaces;
Semaphore elements;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
this.cap = cap;
store = new Object[cap];
spaces = new Semaphore(cap);
elements = new Semaphore(0);
}

// 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) % cap ;
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) % cap ;
spaces.up() ;
return value ;
}
}

and the testbuffer

// Test driver with a single test case for testing Buffer.java

// Simple producer thread that puts 10 items into the buffer
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 1; i < 11; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
p.start();
c.start();
}
}





(e-mail address removed) wrote:
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).


[ snip ]

Java already has a class to do this:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html

So it does. I'm mildly curious about whether they do something more
or less like the OP's code -- not quite curious enough to track down
the source code, but mildly curious.

Writing one's own version is still useful as a learning experience.
 
M

metrix007

Hi there,

Hmmm, I have tried a few approaches, but cant seem to implenet or get
the gist of what your saying, any chance of a code example or pointer
to one? I was unsure about the extra semaphore because I could not
think of a way to implement it.

Cheers
 
B

blmblm

Hi there,

Hmmm, I have tried a few approaches, but cant seem to implenet or get
the gist of what your saying, any chance of a code example or pointer
to one? I was unsure about the extra semaphore because I could not
think of a way to implement it.

Cheers

I'm guessing that you're replying to a previous post of mine, but
without some quoted context, it's hard to be sure. (I suppose
I could check in Google Groups for threading, but why .... )

Your previous code made use of two semaphores. I'm suggesting that
you add a third one, to be used for ensuring one-at-a-time access
to critical shared-among-threads variables. "mutex" is a nice
traditional name for it. The semaphore's initial value should be 1,
and every access to code that manipulates the variables should be
"protected" thus:

mutex.down();

.... code to work on shared variables ....

mutex.up();

There are other ways to do this in Java (using "synchronized"), but
this is the standard semaphore-based solution to the bounded buffer
problem.

Clearer? If you've tried this and it didn't work, post code and
tell us what goes wrong ....
 
M

metrix007

Thanks very much, that seems to be working fine :)

The next stage follow on exercise in my textbook is to create an
implementation of sudoku using dataflow networks and my bounded buffer,
but I was unable to find any information on this.

Any links or explanations would be much appreciated
Cheers
 
B

blmblm

Thanks very much, that seems to be working fine :)
Good!

The next stage follow on exercise in my textbook is to create an
implementation of sudoku using dataflow networks and my bounded buffer,
but I was unable to find any information on this.

Any links or explanations would be much appreciated

So you don't wait and wonder .... I don't think I have any help to
offer with this one (I'm not sure quite what is meant by "dataflow
networks", or by "sudoku"). Maybe someone else will, though. Good
luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top