Commit 7ff3c017 authored by Mirko Stoffers's avatar Mirko Stoffers
Browse files

Made cDynamicExpression::evaluate (and therefore cPar::doubleValue) thread-safe

Problem: cDynamicExpression::evaluate was not thread-safe (also noted in the
comment), but used by cPar::doubleValue from a threaded context. This
caused using par() for random variables to yield wrong results in rare

Solution: Made the global variables used by cDynamicExpression::evaluate


parent 49c66c64
......@@ -264,13 +264,14 @@ inline double trunc(double x)
static const int stksize = 20;
// we have a static stack to avoid new[] and call to Value ctor stksize times
static cDynamicExpression::Value _stk[stksize];
static bool _stkinuse = false;
static __thread cDynamicExpression::Value* _stk = NULL;
static __thread bool _stkinuse = false;
cDynamicExpression::Value cDynamicExpression::evaluate(cComponent *context) const
if(!_stk) _stk=new cDynamicExpression::Value[stksize];
// use static _stk[] if possible, or allocate another one if that's in use.
// Note: this will be reentrant but NOT thread safe
// Note: this will be reentrant and thread safe (due to threadlocal global vars)
Value *stk;
if (_stkinuse)
stk = new Value[stksize];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment