[Telepathy] TelepathyQt threading

Gustavo Boiko gustavo.boiko at canonical.com
Wed Oct 14 06:19:44 PDT 2015


On Wed, Oct 14, 2015 at 8:28 AM, Daniel Pocock <daniel at pocock.pro> wrote:

>
>
> On 13/10/15 22:51, Gustavo Boiko wrote:
> >
> >
> > On Tue, Oct 13, 2015 at 5:39 PM, Daniel Pocock <daniel at pocock.pro
> > <mailto:daniel at pocock.pro>> wrote:
> >
> >
> >
> >     On 13/10/15 22:34, Gustavo Boiko wrote:
> >     > Hi
> >     >
> >     > On Tue, Oct 13, 2015 at 5:18 PM, Daniel Pocock <daniel at pocock.pro
> <mailto:daniel at pocock.pro>
> >     > <mailto:daniel at pocock.pro <mailto:daniel at pocock.pro>>> wrote:
> >     >
> >     >
> >     >
> >     >
> >     >     If a connection manager uses some library that creates its own
> threads
> >     >     (it is not a Qt based library either), are there any
> constraints on how
> >     >     these threads interact with the TelepathyQt API?
> >     >
> >     >     For example, if the connection is completed in another thread,
> is it
> >     >     safe to call setStatus() from that thread?
> >     >
> >     >
> >     > It is probably not safe to call that from a different thread. If
> >     > setStatus() was a Qt slot, you could use
> QMetaObject::invokeMethod()
> >     > with Qt::QueuedConnection directly, but that's not the case. So,
> what
> >     > you can do is to define a slot to proxy the request and use
> >     > QMetaObject::invokeMethod() to call that slot.
> >     >
> >     >
> >
> >
> >     Thanks for the feedback - could you suggest any example of this in
> >     another connection manager or similar code that I can look at?
> >
> >
> > I don't recall seeing another CM with that, but it would be something
> > like this:
> >
> > class MyConnection : public Tp::BaseConnection
> > {
> > ...
> > public Q_SLOTS:
> >     void setStatusSlot(uint newStatus, uint reason) {
> >         setStatus(newStatus, reason);
> >     }
> > }
> >
> > and then, in the other thread you would call:
> > QMetaObject::invokeMethod(myConnectionObject, "setStatusSlot",
> > Qt::QueuedConnection, Q_ARG(uint, status), Q_ARG(uint, reason));
> >
> > That should be more or less what you need to do in your CM.
> >
>
>
> Thanks for that example
>
> The library I'm using (reSIProcate) also lets me run the event loop in a
> thread that I create myself
>
> Does TelepathyQt provide a mechanism to create threads that can be used
> this way and if the threads are created by TelepathyQt, is it safe to
> make calls to things like setStatus() directly?
>

TelepathyQt doesn't have any specific threading capabilities. But Qt does.
You can look for the documentation of QThread as it explains how threading
works in Qt. You can pretty much create this thread using QThread and run
the event loop there, but the way Qt synchronizes threads is through events
(which is what a queued method invocation does in the end), which is why
you need slots.

It would probably be possible to make Tp::BaseConnection thread safe, but I
don't think it is in the scope of TelepathyQt itself, as it only adds extra
complexity with little to no gain (as usually the thread synchronization
would be handle outside of TelepathyQt anyways).

Cheers

Boiko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/telepathy/attachments/20151014/13adbc42/attachment.html>


More information about the telepathy mailing list