c - struct pointer being optimized out using -O3 option -
it seems impossible debug optimized code. i've spent way long trying outsmart compiler. having hard time doing simple check if struct null or not due compiler optimizing code, don't me wrong want keep -o3 option if possible speed code, if keep getting many bugs due compiler optimizations might turn off.
i have thread try dequeue struct entries array , put them database reason struct getting optimized out.
void *queue_func(void *param){ logargs* largs; pthread_mutex_t *mx = (pthread_mutex_t*) param; initqueue(); while(!needquit(mx)){ if((largs = dequeue()) != null){ // boolean result true here interrupt_log(largs->event, largs->rawtime); // yet largs null here!!! } usleep(50000); } return null; }
for reference here dequeue function , struct:
logargs* dequeue(){ logargs* largs; if(isempty()) return null; else{ largs = &queue[++head % max_size]; return largs; } }
here struct:
typedef struct { time_t *rawtime; char event[129]; } logargs;
is there way prevent largs being optimized out?
debugging full optimization - noticed - pain. , tricking volatile
, etc. not much, might faster disable optimizations completely, volatile
inhibits compiler put variable register (for instance).
try -og
(since gcc 4.8). should enable optimizations not interfere debugging.
if have data structures between 2 threads, have tell compiler special. however, modern multi-cpu systems caches, etc. volatile
of little no use application-layer code. use atomic types (stdatomic.h
) instead - supported since gcc 4.9. these include barriers (aka fences) guarantee correct ordering of accesses.
Comments
Post a Comment