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 <an...@familiesomers.nl
<mailto:an...@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@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest