uCalc API Version: 5.7.0-preview.1 Released: 7/30/2026
Warning
uCalc API Preview Release Notice:This preview documentation describes the intended behavior of the API. It is not fully accurate or complete.The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.Use of the preview version in your production code is not recommended.
Explains uCalc's thread safety model and the best practices for using the library in multi-threaded applications.
This guide covers the principles and best practices for using the uCalc library in multi-threaded applications, such as web servers, parallel data processors, or applications with responsive user interfaces.
A single uCalc instance is not thread-safe. An instance is a stateful engine containing variables, functions, error states, and configuration settings. Attempting to access or modify a single instance from multiple threads concurrently without external locking will lead to race conditions, corrupted state, and unpredictable behavior.
Do not share a single uCalc instance across multiple threads.
The correct and most performant approach is to provide each thread with its own dedicated uCalc instance. The recommended pattern is to:
uCalc object with all the required functions, operators, variables, and settings during your application's startup phase.This pattern ensures complete thread isolation, allowing for maximum parallelism without any risk of state corruption.
A key architectural feature of uCalc is that the default instance stack is thread-local. The static DefaultInstance property does not return a single global object; it returns the default instance for the currently executing thread.
This means:
This makes the default instance mechanism surprisingly robust and safe for use in multi-threaded environments, allowing different components or threads to work with their own ambient contexts without conflict.
A developer's first instinct might be to wrap all calls to a shared uCalc instance in a lock or mutex. While this would prevent race conditions, it is strongly discouraged as an anti-pattern.
Using locks serializes access to the `uCalc` engine, creating a major performance bottleneck that completely negates the benefits of multi-threading. The 'one instance per thread' model allows for true parallel execution and is significantly more scalable.## 💡 Why uCalc? (Comparative Analysis)* **Instance-Based vs. Static**: Many simpler libraries provide only static evaluation functions, making them unsuitable for multi-threading due to shared global state. uCalc's instance-based design provides natural encapsulation, making it a perfect fit for parallel processing.* **Efficient Cloning**: The [Clone()](/Reference/uCalcBase/uCalc/Clone) method is lightweight and optimized. It avoids the high cost of creating a new instance and re-running dozens of `Define` calls from scratch for every thread.* **Built-in Thread-Local Default**: The thread-safe default instance is a sophisticated feature that simplifies working with ambient contexts in multi-threaded code, a task that would otherwise require manual implementation of thread-local storage patterns.