Is creating and running Mosquitto clients thread safe?

1,064 views
Skip to first unread message

Eric Hackborn

unread,
Dec 10, 2015, 12:20:29 PM12/10/15
to MQTT
Hi all, I recently integrated Mosquitto into a C++ app but I'm wondering if my design is faulty:

The app is currently using UDP for communication, and I'm experimenting with MQTT. The existing design creates a thread for every socket, and constructs, runs, and destructs the socket in that thread. I used the same pattern for Mosquitto: The Mosquitto lib is initialized and destroyed in the main thread (i.e. mosquitto_lib_init(), mosquitto_lib_cleanup()), but every time I want to create a new Mosquitto client (i.e. mosquitto_new()), I first start a thread, and the client exists solely in that thread.

This works fine in practice, at least so far, but it occurs to me I have no idea if it's safe to be creating and running the Mosquitto clients in different threads than the library was initialized. Does anyone know?

As an aside, I saw that you can run the loop asynchronously. I haven't done that because my main thread is performance critical, so I'd like to keep all processing related to message sending out of it. But if that's the only way to make it async and thread safe, I'll go that route.

thanks much!

ctm

unread,
Dec 10, 2015, 2:22:22 PM12/10/15
to MQTT
As far as I understand MQTT relies on the TCP connection and cannot be deployed on UDP connections.  This even makes MQTT difficult to deploy in pseudo TCP environments like GSM.

To deploy on UDP MQTT-SN is available.  Look at the documentation.  The gateway will connect with a MQTT server using TCP.

C-:

Eric Hackborn

unread,
Dec 10, 2015, 2:26:57 PM12/10/15
to MQTT
Thanks much for the reply, I think I misspoke. The MQTT implementation has nothing to do with the existing UDP communication, it replaces it. I'm just using the same architecture (thread per socket/MQTT client) and was wondering if that's OK.

thanks!

Roger Light

unread,
Dec 11, 2015, 5:06:18 PM12/11/15
to mq...@googlegroups.com
Hi Eric,

A thread per client is fine. If you're mixing threads within a single
client without using loop_start() then you should look at
threaded_set() as well - see the descriptions in mosquitto.h.

Cheers,

Roger
> --
> To learn more about MQTT please visit http://mqtt.org
> ---
> You received this message because you are subscribed to the Google Groups
> "MQTT" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mqtt+uns...@googlegroups.com.
> To post to this group, send email to mq...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages