Sacha Schär wrote:
[color=blue]
> i have a sequence like the following:[/color]
[color=blue]
> const char *a = "foo";
> char b[10];[/color]
[color=blue]
> for ( i = 1; i < strlen(a); i++)
> {
> b[i] = a[i];
> }
> b[0] = a[0];[/color]
[color=blue]
> I guess, an optimizing compiler might mixup the execution
> order of this code snipped. How can I enforce, that the
> copying of the char at position 0 is done after the loop?[/color]
In what sense? The standard requires that the observable
behavior be as if the assignment at position 0 occured after the
loop. But since there is no observable behavior in the above, a
compiler can pretty much do what it wants. Because there is no
observable behavior, of course, you can't tell what it does,
either.
[color=blue]
> I work in a multithreaded environment, and Semaphores, Mutex
> etc. are not suitable for the current situation.[/color]
If other threads can access b, then you cannot avoid some sort
of synchronization primitive. Posix offers mutex, and a few
others; Windows, and many Unix platforms, also have lower level
primitives. But without some primitive, there is no guarantee
concerning the order of the writes as seen in another thread.
(And it's not just a problem of the compiler. Modern hardware
does considerable reordering itself. The primitives will use
special hardware instructions to inhibit the reordering. And
typically, there is no way to get the compiler to emit these
instructions; it must be done in assembler.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]