Created
March 31, 2014 12:05
-
-
Save vadz/9890860 to your computer and use it in GitHub Desktop.
Dirty test for std::ostringstream::imbue() thread safety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <locale.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <locale> | |
#include <sstream> | |
#include <string> | |
static const double TEST_NUM = 1.234567890123; | |
static const int NUM_ITER = 100000; | |
bool check_stream() | |
{ | |
std::ostringstream os; | |
os.imbue(std::locale::classic()); | |
os << TEST_NUM; | |
return os.str().find('.') != std::string::npos; | |
} | |
bool check_printf() | |
{ | |
char buf[32]; | |
snprintf(buf, sizeof(buf), "%g", TEST_NUM); | |
return strchr(buf, ',') != NULL; | |
} | |
void* thread1(void*) | |
{ | |
for ( int n = 0; n < NUM_ITER; n++ ) | |
assert(check_stream()); | |
return NULL; | |
} | |
void* thread2(void*) | |
{ | |
for ( int n = 0; n < NUM_ITER; n++ ) | |
assert(check_printf()); | |
return NULL; | |
} | |
int main() | |
{ | |
setlocale(LC_ALL, "fr_FR"); | |
if ( !check_stream() || !check_printf() ) | |
return -1; | |
pthread_t t1, t2; | |
pthread_create(&t1, NULL, thread1, NULL); | |
pthread_create(&t2, NULL, thread2, NULL); | |
void* ret; | |
return pthread_join(t1, &ret) && pthread_join(t2, &ret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's somewhat reassuring that at least it is fixed in libc++: