[Qt-interest] Fwd: Re: Thread calling Thread and Signal/Slots

Sean Harmer sean.harmer at maps-technology.com
Mon Mar 30 21:49:48 CEST 2009


Hi,

Matthias Pospiech wrote:
> Thiago Macieira schrieb:
>   
>> One more thing: that m_stopped variable is not protected by any mutexes. Is it 
>> volatile?
>>
>>   
>>     
> Yes, it is volatile.
> Does QtSoftware provide an example which uses Signal/Slots within QThreads?
>
> At the moment I only send signals (they are identical in both Threads)
> signals:
>     void changedIteration(int);
>     void finishedCalculation(bool);
>   
If you are only emitting signals from the worker thread then you do not 
need to start an event loop in the worker threads. You only need the 
event loop to receive the signals. Just make sure to use 
QueuedConnection or BlockingQueuedConnection as the type when you make 
the connection. This is all explained in the Qt multi-threading docs:

http://doc.trolltech.com/4.5/threads.html#signals-and-slots-across-threads

Sean
> which are catched in the gui. But my impression was, that if I wanted to 
> catch a signal from the 2nd in the 1st thread I would require an 
> additional loop.
> Especially because then I could catch finishedCalculation(), instead of 
> waiting for 'while (FresnelVariation->isRunning() && (!m_stopped)) ', 
> although I am unsure which approach would be better.
>
>
> Signal/Slots in the GUI:
> -------------------------------------
> void DialogChirpedGrating::connectSignalSlots()
> {
>     connect(ChirpedGratingVariation, SIGNAL(changedIteration(int)), 
> SLOT(OnFresnelVariationChangedIteration(int)));
>     connect(ChirpedGratingVariation, 
> SIGNAL(finishedCalculation(bool)),SLOT(OnFresnelVariationFinished(bool)));
>     connect(ChirpedGratingVariation->FresnelVariation, 
> SIGNAL(changedIteration(int)), 
> SLOT(OnSingleFresnelVariationChangedIteration(int)));   
> }
>
>
> Here is a very short version of the Threads:
> -------------------------------------
> class ChirpedGratingVariationThread : public QThread
> {
>     Q_OBJECT
> public:
>     ChirpedGratingVariationThread()
>         : QThread()
>         , FresnelVariation(new FresnelVariationThread)
>         , m_stopped(false)
>         , m_Iteration(0)
>     {
>
>     }
>     virtual ~ChirpedGratingVariationThread()
>     {
>         delete FresnelVariation;
>     }
>
> public:
>     FresnelVariationThread  * FresnelVariation;
> private:
>     bool volatile m_stopped;
>
>     int m_Iteration;
>
> public:
>      void run()
>     {       
>            Init();
>
>         for (double period = m_Period.start; period <= m_Period.end; 
> period+=m_Period.step)
>         {
>             for (double chirp = m_Chirp.start; chirp <= m_Chirp.end; 
> chirp+=m_Chirp.step)
>             {
>
>                 if (m_stopped)  break;
>                 IterationStep(period, chirp);
>
>                 // wait for FresnelVariation to be finished
>                 while (FresnelVariation->isRunning() && (!m_stopped)) 
>                 {                    sleep(100);                }
>                 m_Iteration++;
>                 emit changedIteration(m_Iteration);
>             }
>         }
>
>         emit finishedCalculation(!m_stopped);
>     }
>
>     void IterationStep(double period, double chirp)
>     {
> ...
>         FresnelVariation->start();
>     }
>
>
> signals:
>     void changedIteration(int);
>     void finishedCalculation(bool);
>
> };
> #endif
> -------------------------------------------------
> class FresnelVariationThread : public QThread
> {
>     Q_OBJECT
> public:
>     FresnelVariationThread(QObject* parent = 0)
>     {
>     }
>     virtual ~FresnelVariationThread()
>     {
>     }
>
>
> public:
>  
>     void run()
>     {       
>         m_stopped = false;
>         Init();
>         Calculation();
>         emit finishedCalculation(!m_stopped);
>         m_stopped = true;
>     }
>     void stop()
>     {
>         m_stopped = true;
>     }
>
> private:
>  
>     void Calculation()
>     {
>        
>         for(double z = m_start; z < m_end; z+=m_step)
>         {           
>             if (m_stopped)
>                 break;
> ...
>             m_Iteration ++;
>             emit changedIteration(m_Iteration);
>         }
>         delete [] Array;       
>     }
>
> private:
>     int m_Iteration;
>     bool volatile m_stopped;
>
> signals:
>     void changedIteration(int);
>     void finishedCalculation(bool);
>
> };
> #endif
>
> ----------------------------------
>
> Matthias
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   




More information about the Qt-interest-old mailing list