#include #include #include #include #include #include #include /* Run this program with LC_ALL=fr_FR.utf-8 (or some other locale with * "," as the decimal point) to demonstrate that c_strtod will affect * other threads on platforms without strtod_l. * * Compile with the -pthread flag if using GCC. */ double c_strtod (char const *nptr, char **endptr) { double r; char *saved_locale = setlocale (LC_NUMERIC, NULL); if (saved_locale) { saved_locale = strdup (saved_locale); if (!saved_locale) abort(); setlocale (LC_NUMERIC, "C"); } r = strtod(nptr, endptr); if (saved_locale) { setlocale (LC_NUMERIC, saved_locale); free (saved_locale); } return r; } void *strtod_thread(void *arg) { int i, use_c_strtod = *((int*)arg); for (i=0; ; ++i) { char *str = use_c_strtod ? "1234.567" : "1234,567"; double val = (use_c_strtod ? c_strtod : strtod)(str, NULL); if (!(fabs(val - 1234.567) < 0.1)) printf("fail val=%f use_c_strtod=%d i=%d\n", val, use_c_strtod, i); } } int main(int argc, char **argv) { pthread_t th[2]; int i, arg[2] = {0, 1}; setlocale(LC_ALL, ""); for (i = 0; i < 2; ++i) { if (pthread_create(&th[i], NULL, strtod_thread, &arg[i])) printf("thread creation failed, i=%d\n", i); } pthread_join(th[0], NULL); return 0; }