strtok(): infamous non-reentrant function
今天看到自己程序中竟然在多线程调用情况下使用了这个函数,太吃惊了。
See here: http://stackoverflow.com/questions/2799023/what-exactly-is-a-reentrant-function
1. Multi-thread
2. Signal handler
Non-reentrant functions:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr) <Re-entrant version>
#include <netdb.h>
#include <sys/socket.h>
struct hostent *gethostbyname(const char *name);
int gethostbyname_r(const char *name,
struct hostent *ret, char *buf, size_t buflen) <Re-entrant version>
struct hostent * gethostbyaddr(const void *addr, int len, int type);
int gethostent_r(
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop) <Re-entrant version>
getservbyname
gerservbyport
#include <time.h>
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
I/O functions are usually not re-entrant
process wide variable: errno, h_errno