[Interest] QThread

Bob Hood bhood2 at comcast.net
Mon Mar 24 16:49:07 CET 2014


On 3/24/2014 9:30 AM, Thiago Macieira wrote:
> Em seg 24 mar 2014, às 16:27:06, André Somers escreveu:
>> 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 using a regular, non-atomic variable could mean that the thread 
> takes a LONG time to see the change, depending on the architecture and OS 
> scheduling.

I actually use the "volatile" tag to tell the compiler that it cannot optimize
(i.e., make compile-time assumptions about) the variable that it decorates. 
Does std::atomic have the same meaning for optimizations?  I can't get a clear
picture of this by a (cursory) Google.

If compilers still consider std::atomic in aggressive optimizing, then it
might be an either/or depending upon particular need.



More information about the Interest mailing list