c - Compiler Optimization, Thread Safe? -
i have question regarding optimizations compiler can potentially do.
the below code speak (this example):
typedef struct test { short i; } s_test; int function1(char *bin) { s_test foo; lock(gmutex); foo.i = *(int*)bin * 8; unlock(gmutex); sleep(5); // // here can happen *bin in thread // inline example here be: *(volatile int *)bin = 42; // int b = foo.i + sizeof(char*); return (b > 1000); } could compiler ever replace last lines with
return ((*(int*)bin * 8 + sizeof(char*)) > 1000); it did not seem case using -o2 or -o3 gcc 4.4 case other compilers , other compilation flags?
i don't think compiler such kind of optimization.
unlock(gmutex) this function, compiler can't assume value pointed bin changed in unlock function or not.
for example, maybe bin comes globe. optimization bin can't cross function call.
Comments
Post a Comment