【无标题】

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()等,或者使用互斥锁等同步机制来保护共享数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值