C 库的非线程安全函数
线程安全函数和非线程安全函数的区别在于是否会修改或返回共享的数据结构。如果一个函数只是读取共享数据而不修改它,那么它就是线程安全的。如果一个函数修改或返回共享数据,那么它就是非线程安全的,除非它使用了同步机制来保证数据的一致性
最初编写CRT函数时,没有多线程技术,所以很多函数内部使用静态变量或者全局变量,随着多线程技术的出现,出现了对应的线程安全的版本。
C库中的大部分函数都是线程安全的,但也有一些例外。一些常见的非线程安全函数有:asctime(), ctime(), gmtime(), localtime(), strtok(), getenv(), gethostbyname(), gethostbyaddr(), getnetbyname(), getnetbyaddr(), getprotobyname(), getprotobynumber(), getservbyname(), getservbyport()
等
以下是非线程安全函数的例子:
-
使用
ctime()
函数返回一个静态内存区的指针,可能会被其他线程覆盖#include <stdio.h> #include <time.h> #include <pthread.h> void *thread_func(void *arg) { time_t t = time(NULL); char *s = ctime(&t); // 非线程安全函数 printf("Thread %d: %s\n", (int)arg, s); return NULL; } int main() { pthread_t t1, t2; pthread_create(&t1, NULL, thread_func, (void *)1); pthread_create(&t2, NULL, thread_func, (void *)2); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }
-
使用
rand()
函数修改一个全局的随机数种子,可能会导致其他线程得到不正确的随机数#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *thread_func(void *arg) { int n = rand(); // 非线程安全函数 printf("Thread %d: %d\n", (int)arg, n); return NULL; } int main() { srand(123); // 设置随机数种子 pthread_t t1, t2; pthread_create(&t1, NULL, thread_func, (void *)1); pthread_create(&t2, NULL, thread_func, (void *)2); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }
-
使用
strtok()
函数修改一个静态的字符串指针,可能会导致其他线程得到不完整的字符串分割#include <stdio.h> #include <string.h> #include <pthread.h> void *thread_func(void *arg) { char *s = "Hello world"; char *p = strtok(s, " "); // 非线程安全函数 while (p) { printf("Thread %d: %s\n", (int)arg, p); p = strtok(NULL, " "); } return NULL; } int main() { pthread_t t1, t2; pthread_create(&t1, NULL, thread_func, (void *)1); pthread_create(&t2, NULL, thread_func, (void *)2); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }
非线程安全函数是指在多线程环境中,可能会导致数据不一致或损坏的函数,通常是因为它们使用了全局或静态变量,或者返回了静态内存区的指针。例如,
rand(), ctime(), localtime()
等函数都是非线程安全的。为了避免非线程安全函数带来的问题,可以使用它们的线程安全版本,如rand_r(), ctime_r(), localtime_r()
等,或者使用互斥锁等同步机制来保护共享数据。