Search code examples
qtmutexsignalssignals-slotsslot

Qt Event processing problem due to QWaitCondition


I am working on a multithread QT project. The main problem is on a serial controller class. This class uses QMutex and QWaitCondition to shyncronize write and read methods.

The problem is about a signal blocked in a children class due to qwaitcondition wait method in the parent. Once the wait method returns, the signal in the children class is emmited (the receiver is also the children class).

I think this method (qwaitcondition - wait) is blocking any signal emission in my entire application.

The problem is something like that:

constructor()
{
    anotherclass = new MyClass;
}
void run()
{
   forever
   {
       if(readmethod())
            waitcondition.wakeall();
   }
}

void method1()
{
      sendpacket();
      mutex.lock();
      if(waitcondition.wait(&mutex,10000))
         //somecode;

      mutex.unlock();
}

class MyClass
{
   someObject sobj;
    MyClass()
    {
        connect(sobj,SIGNAL(somesignal),this,SLOT(someslot));
    }

    void someslot()
    {
        //some code
    }
}

My slot called someslot only is called once the qwaitcondition returns due to the timeout (10 seconds in this case).

I just want to use mutex and the qwaitcondition in this class, not in the entire application. I tried to instantiate the children class in the main thread but I got the same behavior.

CONCEPTUAL TEST

I made a test project and I published it at this link: testwaitconditionproject

the test project works in this way: A waitcondition is iniated with timeout equal to 10 seconds in the parent thread. A children thread emit a signal every 2 seconds, an internal slot must emit a signal captured by the parent. Once the parent slot is called, a flag is set to true. This flag should fire a wakeall to finish de waitcondition.

BUT NOT, the children's internal signals only are fired after the waitcondition timeout. THIS IS MY MAIN PROBLEM, the parent waitcondition is blocking my children's signals.

EDIT:

I solved the problem in the test project, calling the method with waitcondition in another thread. It is not possible to me to replicate the issue because the asynchronous call to the method occurs in the thread 1 (via dbus). I am wondering the dbus call is emitted in the thread 1, so my events in the children class are not going to be emitted while the instance belongs to the same thread.

EDIT 2:

I solved my problem putting the pparent and the children in an intermediate class qthread. they were instantiated in the run method, and an exec call was used too.


Solution

  • It seems that you don't run QCoreApplication event loop, which is required to process events. write() called before QCoreApplication::exec() blocks event loop. Moreover, it seems that it's important to create all QThread objects inside thread which has event loop (if you want to use signals from such objects). I've got your example, created additional thread which runs pparent (and unblocks QCoreApplication::exec()), and it works well.