Optimization/clarity coding : what is gcc able to do for me ?

jlh jlh@gmx.ch
Sat Jan 15 21:22:00 GMT 2005


Robert Dewar wrote:
> There is no rule against the use of shared variables, yes, of course you
> have to be careful, but there are many algorithms that use variables
> that are read by one task and written by another, that work fine. Surely
> you are not claiming that these algorithms are never allowed in C.

Of course not.  It's up to the user to know what he's doing and there's the
volatile keyword to avoid incorrect optimizations.

What I said was only meant for the mentioned situation of a std::vector, which
needs to be in a coherent state in order to be read correctly.  When a thread
manipulates a vector, it might be in an incoherent state for a short time, during
which it would be dangerous for another thread to read this same vector.

That was to point out that it's not up to the compiler to ensure thread-safety.
The optimizer may assume there's only one thread, unless told otherwise, so it's
perfectly valid to move the call to size() out of the loop.

jlh
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20050115/204ab436/attachment.sig>


More information about the Gcc mailing list