c - Segfault after launching a new thread -


i writing stock market system uses several threads process incoming orders.

the project going fine until added 1 more thread. when launch said thread program segfaults. segfault generated in above thread invalid memory read.

this segfault generated only when program compiled optimization -o2 , above.

after compiling programming debug info using -g3 , running valgrind using

valgrind ./marketsim 

and following output segfault

==2524== thread 5: ==2524== invalid read of size 4 ==2524==    @ 0x402914: limitworker (limit.c:4) ==2524==    0x4e33d5f: start_thread (in /lib/libpthread-2.14.so) ==2524==  address 0x1c not stack'd, malloc'd or (recently) free'd ==2524==  ==2524==  ==2524== process terminating default action of signal 11 (sigsegv) ==2524==  access not within mapped region @ address 0x1c ==2524==    @ 0x402914: limitworker (limit.c:4) ==2524==    0x4e33d5f: start_thread (in /lib/libpthread-2.14.so) 

the thread launched this

pthread_t limit_thread; pthread_create(&limit_thread, null, limitworker, q); 

q variable passed other threads initialize

the limitworker code follows

void *limitworker(void *arg){     while(1){         if ((!lsl->empty) && (!lbl->empty)) {             if ((currentpricex10 > lgethead(lsl)->price1) && (currentpricex10 < lgethead(lbl)->price1)) {                 llpairdelete(lsl,lbl);             }         }     }     return null; } 

line 4: line according valgrind produces segfault void *limitworker(void *arg){

also more info compiled using gcc 4.6.1, when using gcc 4.1.2 program doesn't segfault, when optimized although it's performance worse.

when program complied using clang doesn't segfault when optimized.

question

am making mistake?? gcc bug?? course of action should follow??

if want take @ code github page https://github.com/spapageo/stock-market-real-time-system/

the code in question in file marketsim.c , limit.c

edit: valgrind specifies invalid read happens @ line 4. line 4 "head" of function. don't know compiler internals, naive thought argument wrong. but while using gdb after segfault argument , because program optimized, is optimized out according gdb. don't think that culprit.

if compiling 64 bit system, 0x1c offset of price1 field within order struct. implies either (or both) of lsl->head , lbl->head null pointers when fault occurs.

note because limitworker() function includes no thread synchronisation outside of llpairdelete() function, incorrect , compiler may not reloading values on every execution of loop. should using using mutex protect linked lists even in read-only paths.

additionally, lsl , lbl variables multiply defined. should declare them extern in limit.h, , define them without extern in limit.c.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -