[Interest] QThread

André Somers andre at familiesomers.nl
Mon Mar 24 16:27:06 CET 2014


Etienne Sandré-Chardonnal schreef op 24-3-2014 16:11:
>
> volatile is not providing synchronization guarantees in the standard. 
> Even if it may work on most platforms with bool, this is not a good 
> practice.
>
> std::atomic<bool> or QAtomicInt will do the job.
The point is, that usually you don't _need_ synchronization for this 
purpose, and using synchronization does come at a cost. It is usually 
not critical that the worker thread stops immediately when signalled, 
just that it stops soon. So even if - due to missing synchronization and 
the thread reading the flag at the same time it being changed - the 
worker does an other iteration before stopping, nothing is lost. All 
that happens is that the thread does a little bit of work that was not 
needed. The bonus is that the rest of the time, no synchronization is 
needed at all, which may result in better performance.

Note that this only goes for things like such a very simple stop flag. 
For almost all other purposes, you _do_ need synchronization.

André

>
> 2014-03-24 13:40 GMT+01:00 André Somers <andre at familiesomers.nl 
> <mailto:andre at familiesomers.nl>>:
>
>     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.
>
>
>
>
> _______________________________________________
> 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/85598913/attachment.html>


More information about the Interest mailing list