Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] pthreads recommended, optional, ?

Hi Greg,

Pthreads support only applies to the library (and clients, by
extension) at the moment. Pthread support is optional, and there is
quite a bit of overlap between the client and broker code, so when
compiling the broker, or without pthread support, I have macros that
define the pthreads calls away to nothing. It's possible that is where
the redefinition warnings are coming from.

To address your questions though:

>   1) This is sort of a bug report about README not explaining about
>   pthreads.

Fair. I've put some more information on pthreads in the readme.

>   2) Should I add pthreads to mosquitto?  What's it doing without them,
>   when the docs claim it is thread safe?  Is this only about the
>   library?  Does it affect broker behavior?

Yes you should.

The docs are mostly assuming that you're using pthreads, it's not safe
if thread support isn't compiled in. The benefit of adding thread
support to the library is that it enables the `mosquitto_loop_start()`
and `mosquitto_loop_stop()` functions (as well as mutexes everywhere),
which together start and stop a background thread to handle the MQTT
network traffic. This is in contrast to using
`mosquitto_loop_forever()`, which does the same thing but blocks any
further execution, unless you call `mosquitto_disconnect()` in a
callback somewhere. The two approaches allow using the library easily
in two different types of applications, they're both valuable.

The broker is unaffected.

>   3) If yes to 2 does that impose any requirements on library users?

The only requirement is that if the library is being used with threads
not provided by the library, users must use `mosquitto_threaded_set()`
to tell the library this is the case. If the library is being used in
a single threaded environment it makes some optimisations that aren't
valid in a threaded environment. This is one of the first comments in
mosquitto.h.

>   4) If yes to 2 does that change the ABI of the library?  (more or less
>   theoretical as nothing in pkgsrc depends on it

No, the ABI and API remain the same - the only difference is that
`mosquitto_loop_start()` would return MOSQ_ERR_UNSUPPORTED if pthread
support is not compiled in.

> All in all I am getting the impression that pthreads are basically
> required, but not 100%.

Not absolutely required, but definitely very desirable to the end
user. `mosquitto_pub -l` requires it to work, for example.

Regards,

Roger


Back to the top