La funzione GNU asprintf
(stampa su stringa allocata) è thread-safe?È `asprintf` thread-safe?
(IIC, in fondo, questo si riduce a questione se malloc
è thread-safe.)
consideri il codice di esempio:
#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;
}
Ecco, non mi toccare tutte le variabili globali. Cosa succede se il mio getValue
viene chiamato in thread simultanei? Non succederanno cose brutte, vero?
Gli utenti sarebbero condannati se malloc non fosse thread-safe: http://stackoverflow.com/questions/855763/is-malloc-thread-safe – stefan