1

Is there a way to set gui thread priority higher than others threads of my app in Linux? I also know that setPriority function in QThread class, not working in linux. however, is there a solution to do it? (i am working with qt4.8) thanks a lot

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Hossein
  • 95
  • 9
  • Since you have access to the sourcecode you just could pause your threads which results in more time for gui thread. – Sebastian Lange Jul 16 '14 at 06:24
  • i cant do this. because i have another thread that working with socket and get data from network. my program should be able to receive data at any time. as throughput of my socket is high (get image from socket and show on gui), sometimes my gui thread working slowly and in some cases my gui be freeze – Hossein Jul 16 '14 at 06:41
  • 1
    Having a higher priority gui thread would result in the other thread being paused. So its no difference if you manually pause your thread (msecs) or the scheduler pauses your thread – Sebastian Lange Jul 16 '14 at 07:11
  • 1
    if the gui thread freezes then it is blocking on something that it shouldn't often caused by a bad design – ratchet freak Jul 16 '14 at 08:23
  • My recommendation run you application in [PROFILER](http://qt-project.org/doc/qtcreator-2.7/creator-valgrind-overview.html) to find source of problems. I'm for 95% sure that you simply have faulty code. – Marek R Jul 16 '14 at 09:13
  • The suggested dupe isn't the best target - see [QThread::start(priority) vs Linux](/q/55258248/4850040) for one that accurately describes the state of `setPriority()` on Linux. – Toby Speight Oct 25 '22 at 08:28

2 Answers2

1

You shouldn't need to do that. Your GUI should only be displaying the most recent image. Most likely your design forces the GUI to display the outdated images even if they are not relevant anymore.

The typical way this would be achieved in Qt is by having an image viewer class that only displays the most recently set image:

class ImageViewer : public QWidget {
    Q_OBJECT
    QImage m_img;
    bool m_new;
    void paintEvent(QPaintEvent *) {
        QPainter p(this);
        p.drawImage(0, 0, m_img);
        m_new = false;
    }
public:
    ImageViewer(QWidget * parent = 0) : QWidget(parent), m_new(false) {
        setAttribute(Qt::WA_OpaquePaintEvent);
    }
    Q_SLOT void setImage(const QImage & img) {
        if (m_new) qDebug() << "Viewer dropped frame!";
        m_img = img;
        m_new = true;
        if (m_img.size() != size()) setFixedSize(m_img.size());
        update();
    }
};

You can then send signals connected to the setImage slot. Those signals can come from a QObject that has been moved to another thread.

See here for a complete example.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

In GUI thread (for example in main()) execute:

QThread::currentThread()->setPriority(QThread::HighPriority);

See Qt docs for more possible priority values.

Googie
  • 5,742
  • 2
  • 19
  • 31
  • 2
    As i said, i working qt under linux. see again Qt docs for setPriority function ----> The effect of the priority parameter is dependent on the operating system's scheduling policy. In particular, the priority will be ignored on systems that do not support thread priorities (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler for more details). – Hossein Jul 16 '14 at 07:19
  • 2
    @hossein if you read that page that the Qt documentation links to you will see that it says: "The scheduling policy and parameters are in fact per-thread attributes on Linux." This seems to disagree with the note that Qt makes about Linux. I haven't figured out for sure who is right yet but the Qt documentation seems questionable on this point. – altendky Mar 17 '15 at 14:43