GNU函数asprintf
(打印到分配的字符串)是否线程安全?
基本上,这归结为问题是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
在并发线程中被调用会怎么样呢?不会发生坏事,对吗?
malloc
不是线程安全的话,用户将会有问题。 - stefan