zoukankan      html  css  js  c++  java
  • 想在子线程里面触发的信号的槽函数在子线程执行,信号槽连接必须使用DirectConnection 方式(即使跨线程,也可以强迫DirectConnection,而不能是AutoConnection)

    Qt多线程的实现

    1.继承QThread,重新run

    2.继承Object,调用moveToThread方法

    两种方法各有利弊:主要参考:http://blog.51cto.com/9291927/1879757

    在这我主要记录一点:不管是使用那种方式,要想在子线程里面触发的信号的槽函数在子线程执行,信号槽连接必须使用DirectConnection 方式;

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q862343646/article/details/80453938
     
     

    64down voteaccepted

    You won't see much of a difference unless you're working with objects having different thread affinities. Let's say you have QObjects A and B and they're both attached to different threads. Ahas a signal called somethingChanged() and B has a slot called handleChange().

    If you use a direct connection

    connect( A, SIGNAL(somethingChanged()), B, SLOT(handleChange()), Qt::DirectConnection );

    the method handleChange() will actually run in the A's thread. Basically, it's as if emitting the signal calls the slot method "directly". If B::handleChange() isn't thread-safe, this can cause some (difficult to locate) bugs. At the very least, you're missing out on the benefits of the extra thread.

    If you change the connection method to Qt::QueuedConnection (or, in this case, let Qt decide which method to use), things get more interesting. Assuming B's thread is running an event loop, emitting the signal will post an event to B's event loop. The event loop queues the event, and eventually invokes the slot method whenever control returns to it (it being the event loop). This makes it pretty easy to deal with communication between/among threads in Qt (again, assuming your threads are running their own local event loops). You don't have to worry about locks, etc. because the event loop serializes the slot invocations.

    Note: If you don't know how to change a QObject's thread affinity, look into QObject::moveToThread. That should get you started.

    Edit

    I should clarify my opening sentence. It does make a difference if you specify a queued connection - even for two objects on the same thread. The event is still posted to the thread's event loop. So, the method call is still asynchronous, meaning it can be delayed in unpredictable ways (depending on any other events the loop may need to process). However, if you don't specify a connection method, the direct method is automatically used for connections between objects on the same thread (at least it is in Qt 4.8).

    https://stackoverflow.com/questions/15051553/qt-signals-queuedconnection-and-directconnection

  • 相关阅读:
    codeforces 540D Bad Luck Island (概率DP)
    Codevs 1205 单词反转(Vector以及如何输出string)
    Codeforces 977D Divide by three, multiply by two(拓扑排序)
    Codeforces 977B Two-gram(stl之string掉进坑)
    HDU 6186 CS Course (连续位运算)
    HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
    HDU 1004 Let the Balloon Rise(STL初体验之map)
    2018天梯赛、蓝桥杯、(CCPC省赛、邀请赛、ICPC邀请赛)校内选拔赛反思总结!
    Newcoder Wannafly13 B Jxy军训(费马小定理、分数在模意义下的值)
    TCP的可靠传输(依赖流量控制、拥塞控制、连续ARQ)
  • 原文地址:https://www.cnblogs.com/findumars/p/9091054.html
Copyright © 2011-2022 走看看