问题
I'm trying to make a "self contained" communication object allocated in an different Thread, to keep it isolated from the GUI processing delay.
So, in an "AppCore" object creation I create one "CommCore" without parent:
cAppCore::cAppCore(QObject *parent) : QObject(parent)
{
....
CommCore = new cCommCore;
(here I do signal-slot connections between CommCore and AppCore)
....
}
and in the CommCore constructor I do the following:
cCommCore::cCommCore(QObject *parent) : QObject(parent)
{
CommThread = new QThread(this);
CommSerial = new QSerialPort(this);
CommTimer = new QTimer(this);
connect(CommSerial,&QSerialPort::readyRead,this,&cCommCore::ProcessRXByte);
connect(CommSerial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error), this, &cCommCore::HandleSerialError);
connect(CommTimer, &QTimer::timeout, this,&cCommCore::TimerTimeout);
CommTimer->start(OFFLINE_POLL_TIME);
this->moveToThread(CommThread);
CommThread->start(QThread::HighPriority);
}
Now my questions:
1- It's ok to make the Thread handler object a child of the moved object? as it is created in the GUI thread (where the constructor is being called), then moved to the thread that is handled by itself (considering that if I need any control of it I will handle it from inside of the CommCore object)
2- It's ok to start the timer in the constructor (which is in the same thread of the timer constructor, GUI thread), and right after move everything to the new thread?
3- If I wanted to start the timer later, the only way would be thru signal-slot? where a signal emitted in the GUI thread and connected to a CommCore slot (which then would be queued and executed in the CommCore thread)
4- Considering that all my interactions between my CommCore object and the AppCore (GUI thread) are made thru signal-slot (which is thread safe), it is conceptually correct? (the idea of the object move itself and its children to a new thread, etc)
5- My serial communication routines based in pooling (timed by the timer) will be shielded from the GUI processing delays? (I mean, there will be no delays in the generation and processing of events generated by the Serial and Timer objects?)
回答1:
this->moveToThread(CommThread);
will succeed if the object this has no parent. So you need to change the signature of your constructor. Aside of that it is ok, as the object is moved with its children...
The timer should not be started like this, as it is moved into an different thread. You should use invokeMethod such that it is the correct thread which perfoms the operation
QMetaObject::invokeMethod(CommTimer, "start");
For your remaining questions all the answers are yes. As long as you use signals and call using invokeMethod, it is as if the thread is fetching from a pool of runnable and running them.
So thread safe and unaffected by the the main thread interruptions.
来源:https://stackoverflow.com/questions/47693641/qthread-with-qtimer-and-qserial-parenting