eclipse-mosquitto / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
9.14k stars 2.41k forks source link

mosquitto_loop_start(), "This feature is not supported" on Win11, Mosquitto 2.0.14 #2591

Open SwissFrank opened 2 years ago

SwissFrank commented 2 years ago

As I've seen in a closed issue, this function isn't supported on Windows. (BTW this limitation, and the possible return code, aren't documented on https://mosquitto.org/api/files/mosquitto-h.html .)

I've tried the following simplistic workaround and it works fine in a simple test program but dies in a more complicated situation whereby I supply a library running under a third-party piece of software.

The code is something along these lines:

mosquitto_threaded_set(mosq, true);
mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_publish_callback_set(mosq, my_publish_callback);

if (iRC = mosquitto_connect(mosq, host, port, keepalive)) {
    printf("Unable to connect: %s.\n", mosquitto_strerror(iRC));
    return 1;
}
pthrMyTopLoop = new std::thread(mytoploop, mosq);

void mytoploop(struct mosquitto mosq) { int iRC; if (iRC = mosquitto_loop_forever(mosq, -1, 1)) { const char psz = mosquitto_strerror(iRC); printf("mosquitto_loop_forever(): %s\n", psz); return; } }

Basically, my question are:

  1. Should that work? If not why not?
  2. what assumptions the mosquitto library is making about threads? For instance if I call mosquitto_loop_forever() in one thread (after calling mosquitto_threaded_set() true), should I be able to call mosquitto_publish() from different threads? Will my_message_callback() always be called from the thread that called mosquitto_loop_forever()?
  3. In the closed issue you offer some pointers and suggestions if someone is going to try to make it work. I don't want you to take the trouble to explain them this second, but if that's what I need to do, are you still able to supply such pointers and suggestions?

Many Thanks.

SwissFrank commented 2 years ago

Looking at thread_mosq.c, I see it is written exclusively with pthread's. Any particular reason C++11 threads aren't supported? Simply no demand, not a priority?

I'm getting the feeling that:

1) the Win64 distribution is not compiled with WITH_THREADING, and therefore

2) if a user does run the top loop in a separate thread, it will probably "appear" to "work" for a few messages and under light load, but then almost sure to blow up as nothing is locked anywhere. Would you agree?

Would you accept a donation of C++11 threads if someone were to contribute them? (Not offering--yet, just an idea.)

ralight commented 2 years ago

Hi Frank,

Windows thread support is back for the 2.1 release, I don't intend to change anything for 2.0 in that regard. C++11 threads aren't available in C, so that's why they wouldn't be used.

The Windows readme mentions that the threaded functions aren't available in Windows. Unfortunately wrapping mosquitto_loop_forever() in a thread won't work, because the internal locking isn't carried out when compiled without thread support.

Cheers,

Roger

SwissFrank commented 2 years ago

Hi Roger,

Many thanks for the comprehensive reply. My short-term workaround is simply to call mosquitto_loop(mosq,0,1) in a timer event 100x/sec. It doesn't sound ideal but in fact it probably takes <0.1%CPU.

I still maintain a 150,000 line C library written mostly in like 1996 and know the inertia of such projects. C++ APIs are naturally challenging to use in such code. But have you considered having the library itself written in portable C++11, which today seems omnipresently available, and simply making sure the entry points have C bindings? (I'm not talking of rewriting it to be OO, rather, just keeping the code 99% as is and replacing or supplementing the pthread and Windows thread implementations with std::thread calls.) Or are there platforms (embedded systems??) where C++11 simply isn't supported?

Cheers, Frank

On Fri, Jul 22, 2022 at 7:41 AM Roger Light @.***> wrote:

Hi Frank,

Windows thread support is back for the 2.1 release, I don't intend to change anything for 2.0 in that regard. C++11 threads aren't available in C, so that's why they wouldn't be used.

The Windows readme mentions that the threaded functions aren't available in Windows. Unfortunately wrapping mosquitto_loop_forever() in a thread won't work, because the internal locking isn't carried out when compiled without thread support.

Cheers,

Roger

— Reply to this email directly, view it on GitHub https://github.com/eclipse/mosquitto/issues/2591#issuecomment-1192002230, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQXYJTXWNVAEPHH3LEJ5ETVVHGZ7ANCNFSM53433ZUQ . You are receiving this because you authored the thread.Message ID: @.***>

ShrileshKale commented 2 years ago

Hi Roger,

I am also experiencing the same issue in v2.0.14. When do you intend to release V2.1 with support for threads? or is there any older version in achieve that I can use to make mosquitto_loop_start() work in windows?

Cheers, Shrilesh

SwissFrank commented 2 years ago

bump

SwissFrank commented 2 years ago

bump

SwissFrank commented 2 years ago

bump

belragaaasss commented 1 year ago

Hi everyone,

I'm using the version v.2.0.15 and I'm still having the same issues. Any help?

Regards