我在 stackoverflow 問了一個問題 !!!
http://stackoverflow.com/questions/20490058/confusing-core-dump-while-implementing-semaphore
我在測試一個代替 semaphore 的 source , 它的原理不難 , 就是 __snyc_fetch_and_add 比起 mutex 要
花的時間少很多,所以使用 這個 algorithm 在 contentions 不是很大的情況,速度比較快 ,
我測試結果是 , contentions 很大時, 這個 algorithm 慢很多 !!! 這兩個都比不上 seqlock ,
尤其是比不上 buffered seqlock !!!
重點在於 , control -c 程式會 core dump , 現在搞清楚 就是 呼叫的 library 沒有使用 thread-safe 的 header ,
必須使用 -pthread 而非 -lpthread , 使用以下指令 :
gcc -dumpspecs | grep pthread
%{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT}
%{pthread:-lpthread} %{shared:-lc} %{!shared:%{mieee-fp:-lieee} %{profile:-lc_p}%{!profile:-lc}}
%{static: %{fmudflap|fmudflapth: --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc --wrap=mmap --wrap=munmap --wrap=alloca} %{fmudflapth: --wrap=pthread_create}} %{fmudflap|fmudflapth: --wrap=main}
可以看到的是 , -pthread 會加入 -D_REENTRANT 以及 -lpthread , -D_REENTRANT 會使用有 thread-safe 的 header file ,
例如 errno 成為 thread local 變數 , 所以 , 使用 pthread library 應該使用 -pthread 而非 -lpthread !!!!
所以 , 使用 -pthread 之後 , 問題就不見了~~!!!!
留言列表