Home > Mobile >  Qt in the thread of the while loop slot blocking signal problem
Qt in the thread of the while loop slot blocking signal problem

Time:02-15

Everybody:
Encounter a problem has been confused, please comment:
The following was written by a test class
.h
The class WorkPro: public QObject
{
Q_OBJECT
Public:
Explicit WorkPro (parent QObject *=nullptr);
~ WorkPro ();
Void StopThread ();

Signals:
Void reaustReadySignal (int);
Public slots:
Void DoWork ();
Void OnReceivedThreadOperate (int);
Private:
QMutex m_Mutex;
Bool m_bThreadRunFlag;

.cpp relevant code
Void WorkPro: : StopThread ()
{
M_bThreadRunFlag=false;
}

Void WorkPro: : DoWork ()
{
Static int iCount=0;
While (m_bThreadRunFlag)
{
QDebug () & lt; & lt;" The Run DoWrok... "& lt; ICount++;
Emit reaustReadySignal (iCount);
QThread: : msleep (1000);

}
}
Void WorkPro: : OnReceivedThreadOperate (int threadOpera)
{
QDebug () & lt; & lt;" Recvd OnReceivedThreadOperate ";
If (threadOpera==0)
{
QDebug () & lt; & lt;" Pause the Thread ";
}
The else
{
QDebug () & lt; & lt;" Resume Thread ";
}
}



Use this class in the class dialog box, click the button to trigger:

Void Dialog: : on_startBTN_clicked ()
{
M_pWork=new WorkPro ();
M_pWork - & gt; MoveToThread (& amp; M_workThread);
The connect (& amp; M_workThread, & amp; QThread: : finished, m_pWork, & amp; QObject: : deleteLater);
The connect (& amp; M_workThread, & amp; QThread: : started, m_pWork, & amp; WorkPro: : DoWork);
The connect (m_pWork, & amp; WorkPro: : reaustReadySignal, this, & amp; Dialog: : OnReceivedThreadData);
The connect (this, SIGNAL (threadOperateSignal (int)), m_pWork, SLOT (OnReceivedThreadOperate (int)));
M_workThread. Start ();
M_bThreadPauseFlag=true;

}
Void Dialog: : on_pauseBTN_clicked ()
{
If (m_workThread. Set ())
{
If (m_bThreadPauseFlag)
{
Emit threadOperateSignal (0);//0 suspend
QDebug () & lt; & lt;" The Send Pause Signal ";
The UI - & gt; PauseBTN - & gt; SetText (" recovery ");
M_bThreadPauseFlag=false;
}
The else
{
Emit threadOperateSignal (1);//0 suspend
QDebug () & lt; & lt;" Send Resume Signal ";
The UI - & gt; PauseBTN - & gt; SetText (" pause ");
M_bThreadPauseFlag=true;
}
}
}

The question is:
Click on the button to suspend triggered on_pauseBTN_clicked () function
Send signal threadOperateSignal there, I do not receive in WorkPro class,
Reason is that in WorkPro DoWork () used while loop trough,
Confused is I don't sleep in the loop? Sleep is not the CPU time slice possession delivered up,
This event loop is not have a mechanism to perform?
Still say sleep itself is blocking event loop?
Event loop is not QObject control? This powerful a class, how can literally is a child thread while loop to the blocked?

CodePudding user response:

Won't, to call QCoreApplication: : processEvents

In the while maintaining message loop

CodePudding user response:

Moderator: hello, the QCoreApplication: : processEvents I tried once,
Although using it can trigger to the groove function, but there's a good question:
Slot function to trigger the time will be related to sleep in a while loop,
If I sleep in a while for 2 seconds, the trigger delay slot function will be very obvious,
So I don't feel too good this way,

Movetothread approach in the thread + while loop everybody is generally how to solve the communication between the two threads? The while loop is like my two threads are?
(QThread way to inherit the thread is no blocking slot function problems, because that way other slot function belongs to the main thread, the run + while in does not affect)

CodePudding user response:

Online also saw that the thread + timer, this feeling is not too good?
  •  Tags:  
  • Qt
  • Related