[Interest] QThread

Alan Ezust alan.ezust at gmail.com
Mon Mar 24 22:19:07 CET 2014


If you're using Qt 5.2 or later, there is a new API you can use.

Call isInterruptionRequested() in your loop,

 http://qt-project.org/doc/qt-5/qthread.html#isInterruptionRequested

And call requestInterruption() from outside.

http://qt-project.org/doc/qt-5/qthread.html#requestInterruption

I guess this makes it possible for you to avoid the whole atomic/volatile
thing alltogether.



On Mon, Mar 24, 2014 at 5:34 AM, william.crocker at analog.com <
william.crocker at analog.com> wrote:

> 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.
>
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140324/d739a9f5/attachment.html>


More information about the Interest mailing list