GNU 函数asprintf
(打印到分配的字符串)是线程安全的吗?
(IIC,基本上,这归结为是否malloc
是线程安全的问题。)
考虑示例代码:
#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
在并发线程中被调用怎么办?不会有坏事发生吧?