起因:多线程服务器程序在linux平台下各种功能运行正常,但在10-31到11-1时间点切换的时候日志出现了10-1.log,并记录有少量信息,当时就觉得很奇怪,为什么会出现这个问题呢?于是经过多轮测试发现在负荷较大的情况下,生成日志文件确实有问题,并集中出现在跨月的阶段。日志文件名有随机的现象,然后再回复正常。
本着严谨的态度,开始查找原因,因为以前也遇到过线程不安全函数引发的问题,所以很快确定了是localtime函数引起的,在多线程的环境下,localtime并不安全,于是换成了localtime_r,测试程序工作正常。
在linux多线程环境下,对于系统函数的调用,首选还是线程安全函数,否则容易出现比较意想不到的问题,从而浪费大量的精力。
以下列出linux平台下的线程不安全函数, 而他们的安全函数通常为加上后缀_r
asctime()
basename()
catgets()
crypt()
ctermid() if passed a non-NULL argument
ctime()
dbm_clearerr()
dbm_close()
dbm_delete()
dbm_error()
dbm_fetch()
dbm_firstkey()
dbm_nextkey()
dbm_open()
dbm_store()
dirname()
dlerror()
drand48()
ecvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
encrypt()
endgrent()
endpwent()
endutxent()
fcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
ftw()
gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
getc_unlocked()
getchar_unlocked()
getdate()
getenv()
getgrent()
getgrgid()
getgrnam()
gethostbyaddr() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
gethostbyname() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
gethostent()
getlogin()
getnetbyaddr()
getnetbyname()
getnetent()
getopt()
getprotobyname()
getprotobynumber()
getprotoent()
getpwent()
getpwnam()
getpwuid()
getservbyname()
getservbyport()
getservent()
getutxent()
getutxid()
getutxline()
gmtime()
hcreate()
hdestroy()
hsearch()
inet_ntoa()
l64a()
lgamma()
lgammaf()
lgammal()
localeconv()
localtime()
lrand48()
mrand48()
nftw()
nl_langinfo()
ptsname()
putc_unlocked()
putchar_unlocked()
putenv()
pututxline()
rand()
readdir()
setenv()
setgrent()
setkey()
setpwent()
setutxent()
strerror()
strsignal() [Added in POSIX.1-2008]
strtok()
system() [Added in POSIX.1-2008]
tmpnam() if passed a non-NULL argument
ttyname()
unsetenv()
wcrtomb() if its final argument is NULL
wcsrtombs() if its final argument is NULL
wcstombs()
wctomb()
以上函数列表摘自:http://man7.org/linux/man-pages/man7/pthreads.7.html