Loop transformations with potentially shared objects?

As was said in one of the comments to the answer you linked, "the compiler doesn't care about other threads". Unless you have an implementation of std::vector with explicit thread synchronisation constructs, this won't affect optimisation.

The comment in question was made by the fellow arguing that the compiler can't make the transformation. So I end up with "the compiler doesn't care about other threads, but it has to be aware they exist" on one side, and "the compiler doesn't care about other threads, so it can pretend they don't exist on the other side. " I agree with your conclusion, but I'm still lacking the why.

– Dennis Zickefoose yesterday.

By looking only this piece of code, compilers does assume that v is not shared by other threads, and will perform inlining optimizations on size() and operator. There is no indication for compilers to judge this vector may be shared. Also, note that STL is not thread-safe.

If your concern is true (i.e. , v might be modified by other threads), then the code itself would be wrong since vi may have data races. Then, you have to insert a proper critical section.

More correctly, the compiler can assume that v is not modified by other threads. Multiple threads reading an object isn't a problem. – James Kanze yesterday Of course, data races are defined one ore more current accesses while at least one access is write.

– minjang yesterday @James: But why can the compiler assume that? Obviously it is not a safe assumption in general, so it either can't, or the standard gives it permission to somewhere. – Dennis Zickefoose yesterday @Dennis: If a compiler have to assume that a variable can be accessed by other threads, it's really too restrict condition for advanced optimizations.

Of course, compilers are generally doing optimizations in very conservative way. But, I don't think multithreaded accesses are not common case. So, it's reasonable for compilers to assume that a variable does not have data races and no current accesses.

That's why compilers have volatile keyword for your concern. To sum, it's a reasonable trade-off. – minjang yesterday @DennisZickefoose Because that's the way threading is defined.It's up to the programmer to define the granularity, not the compiler.

It really can't be otherwise: what should the granularity be here? It has to be the entire loop, since otherwise, another thread could pop elements off the vector between I And trying to manage atomicity at this level would be extremely expensive (since it would require a compiler generated mutex) and often not needed. – James Kanze 16 hours ago.

In C++03, thread is a library concept not a language concept. Therefore, it is not a requirement for the compiler to care about threads.

If the only standard were C++03, that would be true. Posix imposes some additional constraints, and compilers under Unix machines are expected to be Posix compliant, as well as C++ compliant. A similar situation exists in all environments: compilers are bound by a number of specifications: one or more standards, OS specifications, its own specifications, etc.C++ compilers do "care about threads", at least in environments which support multi-threading.

(This has not always been the case, and I can remember when g++ used static variables in its exception handling). – James Kanze 16 hours ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions