Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:10:06+00:00 2026-05-28T18:10:06+00:00

I need some assistance in Qt on Windows 7. It seems that Qt readyRead()

  • 0

I need some assistance in Qt on Windows 7. It seems that Qt readyRead() signal is emited by an asynchronus procedure call which causes the code to be executed concurrent but in the same thread.

In my example I have a queue which should be accessed by DoRead() and in DoTimer() which is accessed by a lock. Entire operation is running in ui (main) thread. However sometimes as DoRead() is called a dead lock occurred. The code stops execution in DoRead(). The dead lock is reproduceable if the Message Box is showed and so execution of DoTimer() is halted. However I was surprised to see that OnRead() is still called in concurrent. The only explanation for me is, that OnRead() is called by an Windows APC.

See MSDN article Asynchronus Procedure Calls:

An asynchronous procedure call (APC) is a function that executes asynchronously in the context of a particular thread. When an APC is queued to a thread, the system issues a software interrupt. The next time the thread is scheduled, it will run the APC function.

Am I true with my assumption that readyRead() could be an APC?

In either case, what could I do to prevent dead locks? I need to access the queue in DoRead() to fill the queue and in DoTimer() (and other methods of course) to read, write or delete entries from same queue. Recursive mutexes are no solution since both calls occurs in same thread.

class QMySocket : public QTcpSocket {
public:
    QMySocket() {
        ...
        connect(this, SIGNAL(readyRead()), this, SLOT(DoRead()));
        connect(_MyTimer, SIGNAL(timeout()), this, SLOT(DoTimer()));
        ...
    }
private:
    QTimer* _MyTimer;
    QQueue<int> _MyQueue;
    QMutex _Lock;

    void DoRead() {
        _Lock.lock(); // <-- Dead Lock here (same Thread ID as in DoTimer)
        _MyQueue... // Do some queue operation
        // DoSomething
        _Lock.unlock();
    }

    void DoTimer() {
        _Lock.lock();
        QQueue<int>::iterator i = _MyQueue.begin();
        while (i != _MyQueue.end()) { // Begin queue operation
            if (Condition) {
                QMessageBox::critical(...);
                i = _MyQueue.erase(i);
            } else {
                i++;
            }
        } // end queue operation
        _Lock.unlock();
    }
};

Edit 2: This had nothing to do with APC as I found out. The problem was only the extra message loop created by QMessageBox.

Instead calling QMessageBox directly, all messages will be queued and showed after any queue operation.

void DoTimer() {
    QList<QString> Messages;
    QQueue<int>::iterator i = _MyQueue.begin();
    while (i != _MyQueue.end()) { // Begin queue operation
        if (Condition) {
            Messages.append(...);
            i = _MyQueue.erase(i);
        } else {
            i++;
        }
    } // end queue operation
    QMessageBox::critical(Messages);
}

Locks are not required if there is no concurrent access to the queue (no multithreading).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:10:06+00:00Added an answer on May 28, 2026 at 6:10 pm

    Your only problem is the call to

    QMessageBox::critical(...);
    

    This call blocks until you press a button. But since you called it while still holding the lock, your DoRead deadlocks.

    There is absolutely no reason to open a messagebox while holding that lock!

    If you still want your DoTimer to respond while showing the messagebox don’t use the static convenience methods like QMessagebox::critical.

    Better do this

       // Somewhere in the constructor ...
       QMessageBox* msgBox = new QMessageBox( this );
       msgBox->setAttribute( QWidget::WA_DeleteOnClose );
       msgBox->setStandardButtons( QMessageBox::Ok );
       msgBox->setWindowTitle( tr("Error") );
       msgBox->setModal( true );
       //...
    
    void DoTimer() {
        _Lock.lock();
        // DoSomething
        _MyQueue... // Iterate over queue, and do some queue operation (delete entires for exmaple)
        _Lock.unlock();
        msgBox->setText( tr("DingDong!") );
        if (!msgBox->isVisible())
            msgBox->open( this, SLOT(msgBoxClosed(QAbstractButton*)) );
    }
    
    void MyWidget::msgBoxClosed(QAbstractButton*) {
       qDebug("Byebye msgbox");
    }
    

    But still, from your code I don’t see any reason to use mutexes anyway. There is no concurrency, right?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've encountered an issue that is IE9-specific and need some assistance in order to
I am new to jQuery and I need some assistance with very simple code.
I've been given a problem that I need some assistance from the SO community
I need some assistance with this code: http://jsfiddle.net/N5xTJ/1/ The last column already is dynamic
Here is my working image upload and rename code, I however need some assistance
I need some assistance from the Flex experts. I have an mx:Menubar , which
I have an incomplete query that I need some assistance with. It currently runs
I am working on some simple form validation and need some assistance. Basically, I
I need some jQuery assistance. I'm using https://github.com/jbutz/bootstrap-lightbox for my lightbox, basically what I
I'm in need of some assistance with Ruby on Rails and Sqlite3. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.