Blocking queue

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Blocking queue

gyyk
Hi,

Conceptually, this is what I'd like to achieve.
Suppose I have two types of tasks they are put in some
kind of queue.

I want to have some number of threads desginated to
block on take() only on type 1 task.

I also want to have some number of threads designated
to take type 2 task and process.
However, if none of the type 2 tasks is available,
then it should take the type 1 task and starts
processing.

Is there any neat way of doing it besides making two
queues and poll on them alternatively?

Thanks!

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Blocking queue

Dawid Kurzyniec
Grace Kwok wrote:

> Hi,
>
> Conceptually, this is what I'd like to achieve.
> Suppose I have two types of tasks they are put in some
> kind of queue.
>
> I want to have some number of threads desginated to
> block on take() only on type 1 task.
>
> I also want to have some number of threads designated
> to take type 2 task and process.
> However, if none of the type 2 tasks is available,
> then it should take the type 1 task and starts
> processing.
>
> Is there any neat way of doing it besides making two
> queues and poll on them alternatively?
>  

I'd implement it by writing a custom blocking queue implementation (not
necessarily implementing the BlockingQueue interface) using two
non-blocking queues internally and providing its own wait/notification
mechanisms on top of that, using condition variables. For instance,
type1queueNotEmpty, anyQueueNotEmpty, type1queueNotFull, and
type2queueNotFull might be your conditions; the put(type2obj) would
await on type2queueNotFull, then put, then signal anyQueueNotEmpty. Your
"takeType1task()" would wait on type1queueNotEmpty, then take, then
signal type1queueNotFull, etc.

Regards,
Dawid

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Blocking queue

gyyk
In reply to this post by gyyk
Presumably, the takeType2BeforeType1Task() method
waits for anyQueueNotEmpty.  But the one who signals
it is the put(type2obj).  So, the
takeType2BeforeType1Task() method would really only be
the takeType2Task() method if I understand correctly.

Thanks, Grace

On 5/17/06, Dawid Kurzyniec <[hidden email]>
wrote:


    I'd implement it by writing a custom blocking
queue implementation (not
    necessarily implementing the BlockingQueue
interface) using two
    non-blocking queues internally and providing its
own wait/notification
    mechanisms on top of that, using condition
variables. For instance,
    type1queueNotEmpty, anyQueueNotEmpty,
type1queueNotFull, and
    type2queueNotFull might be your conditions; the
put(type2obj) would
    await on type2queueNotFull, then put, then signal
anyQueueNotEmpty. Your
    "takeType1task()" would wait on
type1queueNotEmpty, then take, then
    signal type1queueNotFull, etc.

    Regards,
    Dawid

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Blocking queue

Dawid Kurzyniec
Grace Kwok wrote:
> Presumably, the takeType2BeforeType1Task() method
> waits for anyQueueNotEmpty.  But the one who signals
> it is the put(type2obj).  So, the
> takeType2BeforeType1Task() method would really only be
> the takeType2Task() method if I understand correctly.
>
>  
The put(type1) may signal _both_ "nonEmpty" conditions (they belong to
the same lock).

Regards,
Dawid

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest