NCalc Thread Safety?

Topics: General
Nov 9, 2011 at 10:08 AM
Edited Nov 9, 2011 at 10:09 AM


I'm reading a arrays of data from a data source, and I'd like to run the same NCalc operation on the data sets.

It's not so hard to just loop and do the calculation sequentially, but this seems like a perfect case for me to use something like PLINQ (or just normal threading) to try to do as many of the NCalc function calls in parallel as possible.

I can't see any information on the thread safety of NCalc though - will this work?


Nov 9, 2011 at 5:52 PM

There is a static cache that is thread safe, and every instance of Expression is too. So in theory if each of your tasks are instantiating their own Expression, there should be no issue at all. I think there is also a Unit Test for that in the source code if I remind correctly.

Nov 9, 2011 at 11:02 PM

Each of my tasks will run the same expression in parallel.

And it sounds like that will work, which is great, thanks.

I suppose my only concern was that if I start up multiple theads, they will each try to do the "once off" compilation of the expression, and get confused. I think that's what you mean by static cache, is that right?