1. signals and slots in the Qthread class
(1) Qthread class has the ability to transmit signals and define slot functions
(2) Key signals in the Qthread
①void started (): emits the signal when the thread starts to run
②void finished (): the signal is emitted when the thread finishes running
③void terminated (): The signal is emitted when the thread is terminated abnormally
2. thread-related Concepts
(1) Thread Stacks
The concept of stack space exists in the ① process (different from the stack data structure)
② stack space is dedicated to function calls ( save function arguments, local variables, etc. )
③ thread has a separate stack space (other functions can be called)
④ as long as there is no code to access the critical resource in the function body, the same function can be called simultaneously by multiple threads without any side effects.
(2) process PID and Thread Tid
① process Pid: The process has a globally unique ID Value. Qint64 qprocess::p rocessid () const
② thread tid: thread has a unique ID value within the PROCESS.
(3) key static member functions in Qthread
①qt::handle currentthreadid ()
②qthread* currentthread ()
3. on which thread is the slot function executed by multiple threads?
(1) Automatic connection : If the thread of the transmitting signal and the thread of the slot function object are the same threads, the Direct connection is adopted by Default. If it is on a different thread , a queue connection is Used.
(2) Direct connection (directly Connection): indicates that the slot function is to be called immediately and directly ( which can be understood as the way of using send ) . so, Whoever emits the signal will receive the signal (execution slot function), that is, the thread that emits the signal to emit the signal, and also to execute the slot Function.
(3) Queue connection (Queued Connection): indicates that the signal is delivered (post) to the event queue of the thread where the slot function object Resides. so, of course, the slot function should be executed by the thread of the object on which it resides .
The run context for the "programming experiment" slot function : How many threads do the slot function execute on?
MyObject.h
#ifndef Myobject_h#defineMyobject_h#include<QObject>classMyObject: publicQobject{q_object public: ExplicitMyObject (qobject *parent =0); signals:protectedslots:voidGetstarted ();//slot function to start the response thread voidGetfinished ();//slot function for response thread end};#endif //Myobject_h
MyObject.cpp
#include"MyObject.h"#include<QThread>#include<QDebug>Myobject::myobject (qobject*parent): qobject (parent) {}voidmyobject::getstarted () {qdebug ()<<"void myobject::getstarted () tid ="<<Qthread::currentthreadid ();}voidmyobject::getfinished () {qdebug ()<<"void myobject::getfinished () tid ="<<Qthread::currentthreadid ();}
TestThread.h
#ifndef Testthread_h#defineTestthread_h#include<QThread>classTestthread: publicQthread{q_objectprotected: voidRun (); public: ExplicitTestthread (qobject* parent =0); ~testthread (); signals:voidtestsignal ();protectedslots:voidTestslot ();};#endif //Testthread_h
TestThread.cpp
#include <QDebug>#include"TestThread.h"Testthread::testthread (qobject*parent): qthread (parent) {connect ( this, SIGNAL (testsignal ()), this, SLOT (testslot ()));}voidtestthread::run () {qdebug ()<<"void Testthread::run ()--begin tid ="<<Currentthreadid (); for(intI=0; i<Ten; i++) {qdebug ()<<"void Testthread::run () i ="<<i; Sleep (1); } Emit testsignal (); //The custom signal is emitted by a child thread, in this case the signal and slot are the default connections, because the Slot function object//in the main thread, there are two different threads, with a queue Connection. So the slot function is executed by the main thread. Qdebug ()<<"void Testthread::run ()--end";}voidtestthread::testslot () {qdebug ()<<"void Testthread::testslot () tid ="<<Currentthreadid ();} testthread::~testthread () {}
Main.cpp
#include <QCoreApplication>#include<QThread>#include<QMutex>#include<QDebug>#include"MyObject.h"#include"TestThread.h"//Custom message handlers let the Qdebug output be thread-safe. voidMessageoutput (qtmsgtype type,ConstQmessagelogcontext &context,ConstQString &Msg) {qbytearray localmsg=Msg.tolocal8bit (); //fcntl (stderr,); StaticQmutex mutex; Mutex.Lock(); Switch(type) { caseqtdebugmsg:fprintf (stderr,"%s\n", Localmsg.constdata ()); break; caseqtinfomsg:fprintf (stderr,"Info:%s (%s:%u,%s) \ n", Localmsg.constdata (), context.file, context.line, context.function); break; caseqtwarningmsg:fprintf (stderr,"Warning:%s (%s:%u,%s) \ n", Localmsg.constdata (), context.file, context.line, context.function); break; caseqtcriticalmsg:fprintf (stderr,"Critical:%s (%s:%u,%s) \ n", Localmsg.constdata (), context.file, context.line, context.function); break; caseqtfatalmsg:fprintf (stderr,"Fatal:%s (%s:%u,%s) \ n", Localmsg.constdata (), context.file, context.line, context.function); Abort (); } mutex.unlock ();}intMainintargcChar*Argv[]) {qcoreapplication a (argc, argv); //Install message handlersQinstallmessagehandler (messageoutput); Qdebug ()<<"main () tid ="<<Qthread::currentthreadid (); Testthread t; MyObject m; //M.movetothread (&t);//Here you can uncomment to demonstrate execution of getstarted and getfinished () two//the difference between the threads of a slot function//default connection: when M.movetothread (&t), m is the same thread as the sub-thread that emits the signal, with a direct connection. //when M.movetothread is not executed, the launch information thread is not the same thread as the M threads, so the queue connection is Used. //qobject::connect (&t, SIGNAL (started ()), &m, SLOT (getstarted ())); //qobject::connect (&t, SIGNAL (finished ()), &m, SLOT (getfinished ())); //Queue connection: indicates that the signal is posted to the object thread message queue where the slot function Resides.//in this example, the transmit signal thread (sub-thread) throws the signal into the Slot function object (M)Qobject::connect (&t, SIGNAL (started ()), &m, SLOT (getstarted ()), qt::queuedconnection); //Direct connection: indicates that the slot function is called directly. So who is going to be the one to receive (execute the slot function). This example is emitted by a child thread and should also be received by a child thread)Qobject::connect (&t, SIGNAL (finished ()), &m, SLOT (getfinished ()), Qt::D irectconnection); T.start (); returna.exec ();}/*output: main () tid = 0x428void testthread::run ()--begin tid = 0x2280void myobject::getstarted () tid = 0x428void Test Thread::run () i = 0void testthread::run () i = 1void testthread::run () i = 2void testthread::run () i = 3void testthread :: Run () i = 4void testthread::run () i = 5void testthread::run () i = 6void testthread::run () i = 7void Testthread::run ( ) i = 8void Testthread::run () i = 9void testthread::run ()--endvoid testthread::testslot () tid = 0x428void myobject::g etfinished () tid = 0x2280*/
4. Summary
(1) Qthread class has the ability to transmit signals and define slot functions
(2) a thread has a unique ID value within the process
(3) thread has a separate stack space for function calls
(4) a function without a critical resource can be called by multiple threads without side effects
(5) the call of the slot function is done in one thread .
Lesson 78th signals and slots in multi-threading (TOP)