[rabbitmq-discuss] Multi-threading in c/c++

Alan Antonuk alan.antonuk at gmail.com
Fri Feb 17 01:27:23 GMT 2012


Response inlined:

-Alan

On Thu, Feb 16, 2012 at 7:13 AM, Eric J. Holtman <eric at holtmans.com> wrote:

> I've been hoping to get rabbitmq integrated into some
> of my applications.  I've read a few books and web
> articles.  I've wrangled the source into compiling and
> working under Windows.  The examples run fine.
>
> I'm trying to figure out how to run under a multithreaded
> environment.  As I understood it, the whole point of having
> a multiplexed connection was so that I didn't have to
> burn an actual TCP connection for every channel.
>
> However, that doesn't seem to be how rabbitmq-c is
> set up.
>
> In fact, a recent question from the archives, answered
> by Alan says:
>
> > You have interpreted the documentation correctly:
> >
> > amqp_connection_state_t, its socket, and all associated amqp_channel_t's
> > must not be shared between threads.  Its ok to have separate instances of
> > these objects used in different threads.
>
> So, if I want to consume on 4 queues, and the natural
> solution would be to have 4 threads doing a blocking
> wait (like "run" in the amqp_consumer example), I have
> to have a separate TCP connection for each one?
>

Thats one way to do it.  Another would be to a have a "connection thread"
that would own the non-thread-safe objects, and would dispatch the messages
to the worker threads.  It takes a bit more code to accomplish it but its
similar to how the Java and .NET clients work, which have a thread that
handles events on a connection level, then the Channel objects associated
with the Connection object can be on different threads.

>
> Also, I assume if I'm going to use a c++ wrapper like
> amqpcpp or rabbitcpp I'm going to have the same issue,
> since they're using rabbitmq-c as the lower layer?
>

Having not used either of those C++ wrappers I believe your assumption is
correct, unless the implement their own event-loop or thread that
dispatches consumed messages.

As a self-less plug, if you're looking for a C++ wrapper - I'd encourage
you to checkout SimpleAmqpClient https://github.com/alanxz/SimpleAmqpClient,
which is Yet Another rabbitmq-c wrapper.  It won't necessarily solve your
threading question though.

>
> Thanks,
>
> Eric
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120216/f7da9778/attachment.htm>


More information about the rabbitmq-discuss mailing list