[Interest] QThread

André Somers andre at familiesomers.nl
Mon Mar 24 13:40:15 CET 2014


william.crocker at analog.com schreef op 24-3-2014 13:34:
> On 03/24/2014 08:10 AM, Graham Labdon wrote:
>> Thanks for that - my ui is now responsive
>> What is the correct way to stop my thread running?
>> I tried m_thread->exit() but it still continues
>>
> Have it periodically check a global, (mutex-protected?)
> variable and then terminate (by returning from the run()
> function) voluntarily.
Protecting it with a mutex is usually not needed, and it doesn't need to 
be global either. However, you may want to use a QAtomicInt or a 
volatile bool as your flag type, and you can make that a member variable 
of your worker class that can be set with a simple method call.

André

>
>> Thanks again
>>
>> -----Original Message-----
>> From: interest-bounces+graham.labdon=avalonsciences.com at qt-project.org [mailto:interest-bounces+graham.labdon=avalonsciences.com at qt-project.org] On Behalf Of André Somers
>> Sent: 24 March 2014 12:06
>> To: interest at qt-project.org
>> Subject: Re: [Interest] QThread
>>
>> Graham Labdon schreef op 24-3-2014 12:38:
>>> Hi
>>> I am experimenting with QThreads
>>> The basic idea is to have a thread performing some task and to have a
>>> button on the UI that can stop this operation
>>>
>>> So I have a class that will perform the operation called
>>> GeneratorThread that has a start slot In the main ui class I create an
>>> instance of the class -
>>>
>>> m_generatorThread = new GeneratorThread(this);
>>>
>>> Then I call moveToThread  and make the connections-
>>> m_generatorThread->moveToThread(m_thread)
>>> connect(m_thread,&QThread::started,m_generatorThread,&
>>> GeneratorThread::start);
>>> connect(m_generatorThread,&GeneratorThread::finished,m_thread,&QThread
>>> ::quit);
>>> connect(m_generatorThread,&GeneratorThread::finished,m_generatorThread
>>> ,&GeneratorThread::deleteLater);
>>> connect(m_thread,&QThread::finished,m_generatorThread,&GeneratorThread
>>> ::deleteLater);
>>>
>>> And finally I start the thread
>>> m_thread->start()
>>>
>>> The generatorThread simply writes a debug message every second and I see this happening.
>>> The problem is that my ui does not respond to any input - so my stop
>>> button will not work
>>>
>>> I am obviously doing something wrong and would appreciate any help
>> Yeah, you are doing some things wrong.
>> Your moveToThread will fail, because GeneratorThread (confusing name for what seems to be a worker class?) has a parent. That causes that your worker is not in a different thread at all, but blocking your main thread instead.
>>
>> André
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list