google / google/double-conversion

DoubleToStringConverter::EcmaScriptConverter() not thread safe on Windows

Open
#3 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.2k
Forks
308
Avg merge
2d 9h
Merged PRs (30d)
12

Description

From http://code.google.com/p/double-conversion/issues/detail?id=30 :

-- sean.parent
Code inspection at: double-conversion.cc:44

Function level statics are not initialized in a thread safe manner with Visual Studio 2012 or prior. With gcc and clang thread safe initialization is optional (though required by C++11).

Could be fixed with any number of once init mechanisms, or aggregate initialization.

-- floitsch
I don't see any easy way of solving this without pulling in thread-specific headers.
I would rather just require -std=c++0x which, afaik, guarantees correct initialization.
I will discuss this with my colleagues next week and see if somebody comes up with a better solution.

-- stephen.mercer
If you simply move the function static out of the function and make it static to the file then it will be thread safe initialized. Reference this answer:
http://stackoverflow.com/questions/1962880/is-c-static-member-variable-initialization-thread-safe

So to make it thread safe, simply replace this code (and similar if you have other function-level statics):

```
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
static DoubleToStringConverter converter(flags,
"Infinity",
"NaN",
'e',
-6, 21,
6, 0);
return converter;
}
```

with this code:

```
static DoubleToStringConverter kConverter(UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN,
"Infinity",
"NaN",
'e',
-6, 21,
6, 0);

const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
return kConverter;
}
```

-- rafaelefernandez
You definitely do not want to move it out of the function because then you will run into the static initialization order problem.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.