Re: Invoking GTK main thread from other threads
Daniel Boles via gtkmm-list <[email protected]> Thu, 10 Sep 2020 18:35:45 +0100
| Newsgroups | gmane.comp.gnome.gtkmm |
|---|---|
| Message-ID | <CAKChMKNCDCOYb2qE8iE9WePOTwTKxJz5hgfLVkvL_hoekiOuBQ@mail.gmail.com> |
That said, it's equally possible I am wrong and SignalIdle technically is not thread-safe. I can't really make sense of the wording in the docs... for connect_once(), we have: > Because sigc::trackable <http://library.gnome.org/devel/libsigc++/unstable/structsigc_1_1trackable.html> is not thread-safe, if the slot represents a non-static method of a class deriving from sigc::trackable <http://library.gnome.org/devel/libsigc++/unstable/structsigc_1_1trackable.html>, and the slot is created by sigc::mem_fun() <http://library.gnome.org/devel/libsigc++/unstable/group__mem__fun.html#gadf6b6d22c503b439019f0a2e77352419>, connect_once() <https://developer.gnome.org/glibmm/stable/classGlib_1_1SignalIdle.html#a94217dc63e35f96b5cee4c48f2cdd020> should only be called from the thread where the SignalIdle <https://developer.gnome.org/glibmm/stable/classGlib_1_1SignalIdle.html> object's MainContext <https://developer.gnome.org/glibmm/stable/classGlib_1_1MainContext.html> runs. You can use, say, boost::bind() or, in C++11, std::bind() <http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01513.html#ga7b2eddb726568256e49824ad01a05099> or a C++11 lambda expression instead of sigc::mem_fun() <http://library.gnome.org/devel/libsigc++/unstable/group__mem__fun.html#gadf6b6d22c503b439019f0a2e77352419> . That makes enough sense and still leaves plenty cases for which it'll definitely be safe. but for connect(), we have the unconditional: > This method is not thread-safe. You should call it, or manipulate the returned sigc::connection <http://library.gnome.org/devel/libsigc++/unstable/structsigc_1_1connection.html> object, only from the thread where the SignalIdle <https://developer.gnome.org/glibmm/stable/classGlib_1_1SignalIdle.html> object's MainContext <https://developer.gnome.org/glibmm/stable/classGlib_1_1MainContext.html> runs. I would expect the same condition(s) to apply to connect() as to connect_once() - since AFAIK, all the once version does is just disconnect itself automatically - but I might just be missing some obvious reason why this can't be the case. And now I feel like maybe I've had this discussion in the past...! Anyway, I mentioned it because I have had the usual crashy behaviour symbolic of trying to access widgets in threads other than the main one, in my own projects, and wrapping the code doing said accesses in a lambda run in SignalIdle has always fixed it. Maybe I've just been lucky so far? Thoughts/corrections to me from those more experienced at doing multi-threading with gtkmm are welcome. _______________________________________________ gtkmm-list mailing list [email protected] https://mail.gnome.org/mailman/listinfo/gtkmm-list