[Ace-users] [ace-users] Different Reactors for reading and writing

Steve Huston shuston at riverace.com
Wed Feb 6 12:03:49 CST 2008

Hi Gonzalo, good to hear from you.

>          5.4.7

Oy... ;-)

>          Ubuntu linux 2.6.22 (7.10)
> ...
> This is more a question to enlighten my ignorance, than a 
> proper problem
> report... I have a single-threaded process that intends do do this:
> 1. Have an ACE_Acceptor and an ACE_Connector.
> 2. The ACE_Connector will connect to the ACE_Acceptor using
>    non-blocking IO (one thread). A Reader and Writer (both
>    inheriting from ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>)
>    will be created.
> 3. There will be an ACE_Message_Queue<ACE_MT_SYNCH> on which 
> the Writer
>    will enqueue messages and from which Reader will dequeue
>    This will happen in random order.
> The problem I am seeing is that if the Writer hits the queue high
> watermark, the call to enqueue() will simply block; this will not be
> good, since the Reader (running on the same thread) will never get
> chance to dequeue a message and make more space for the 
> Writer to go on.

Right, because you specified ACE_MT_SYNCH on the queue and the default
time argument is 0 (block).

> One solution for this could be to query whether the queue is_full()
> before trying to enqueue another message, but that just moves the
> control issue somewhere else. Then I thought about maybe using two
> Reactors running on two separate threads, one for the 
> Acceptor / Reader
> and one for the Connector / Writer. My questions are:
> 1. Is this the proper way to handle this situation? Is it correct to
>    assume that if I want the Acceptor / Reader to run on a separate
>    thread from the Connector / Writer, then I must have two separate
>    Reactors, one for each of them? If I am wrong and I could have a
>    single Reactor running on two threads, how would I set the
>    Reactor's owner?

You're mixing apples and oranges... If you really want one thread, you
should change the queue/svc_handler synch trait to ACE_NULL_SYNCH.
Then the enqueue will fail at high-water mark rather than block.

If you want more than one thread and/or multiple reactors, study the
options for this in C++NPv2 chapter 4.

> 2. Is it Ok to have more than one Reactor on a single program? Will
>    run into problems if I go this way?

You can do this.

> 3. Are there any other suggested ways to handle this use 
> case? Could the
>    TP_Reactor play any part in this?

If you want multiple threads driving events, that's a popular
approach. Check out C++NPv2 chapter 4 for all the options though.

Best regards,

Steve Huston, Riverace Corporation
Want to take ACE training on YOUR schedule?
See http://www.riverace.com/training.htm

More information about the Ace-users mailing list