是'asprintf`线程安全的? [英] Is `asprintf` thread-safe?

查看:240
本文介绍了是'asprintf`线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是GNU功能 asprintf (打印分配的字符串)是线程安全的?

Is the GNU function asprintf (print to allocated string) thread-safe?

(IIC,基本上,这归结为这个问题是否的malloc 是线程安全的。)

(IIC, basically, this boils down to the question whether malloc is thread-safe.)

考虑例如code:

#define _GNU_SOURCE
#include <stdio.h>

#include "getValue.h"

char * getValue(int key) {
  char * value;
  asprintf(&value, "%d", key); // TODO: No error handling!
  // If memory allocation wasn't possible, or some other error occurs,  these  functions  will
  // return -1, and the contents of strp is undefined.
  return value;
}

在这里,我不碰任何全局变量。如果我的的getValue 获取并发线程叫什么?没有不好的事情会发生,是吗?

Here, I do not touch any global variables. What if my getValue gets called in concurrent threads? No bad things will occur, will they?

推荐答案

是的,它是线程安全的除非它读取的语言环境。

Yes, it's thread safe except when it reads the locale.

asprintf

功能:INT asprintf(焦** PTR,为const char *模板,...)结果
  preliminary:| MT-安全区域| AS-不安全堆| AC-不安全纪念品

Function: int asprintf (char **ptr, const char *template, …)
Preliminary: | MT-Safe locale | AS-Unsafe heap | AC-Unsafe mem

关于区域的<一个href=\"https://www.gnu.org/software/libc/manual/html_node/Other-Safety-Remarks.html#Other-Safety-Remarks\"相对=nofollow>例外,特别是:

功能标注了区域从Locale对象没有任何形式的同步读取MT-安全问题。与区域与区域的变化同时调用注解功能的行为可能在其执行过程中不对应任何活动的区域设置的方式,但如果没有predictable混合体。

Functions annotated with locale as an MT-Safety issue read from the locale object without any form of synchronization. Functions annotated with locale called concurrently with locale changes may behave in ways that do not correspond to any of the locales active during their execution, but an unpredictable mix thereof.

这些类型的功能被称为<一href=\"https://www.gnu.org/software/libc/manual/html_node/Conditionally-Safe-Features.html#Conditionally-Safe-Features\"相对=nofollow>有条件多线程安全的,因为,在某些情况下,他们变成毁灭,所以程序员需要采取的照顾。

These kinds of functions are known as "conditionally" multithread safe because, in some contexts, they turn out not to be, so the programmer needs to take care of that.

这篇关于是'asprintf`线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆