Update 23.11.2015: The latest version is now part of the official QuantLib Release 1.7.
Update 22.09.2015: Please find the latest version of the patch for QuantLib 1.6.2 here.
Update 28.02.2015: Please find the latest version of the patch for QuantLib 1.5 here.
Update 11.05.2014: Please find the latest version of the patch for QuantLib 1.4 here.
QuantLib is per se not thread-safe. The standard way to utilise more than one core is to spawn several independent processes. Riccardo’s thread-safe singleton patch allows to use QuantLib within multi-threading applications as long as objects aren’t explicitly shared between different threads. In fact this patch turns the singleton pattern into a thread local singleton pattern. One possible use case of this patch is to run the test-suite with a multi-threading test runner to speed it up, e.g. on a i7@3.2Ghz with four cores plus four HT cores the multi-threading test-suite runs in approx. two minutes whereas the single threaded version takes around eight minutes.
Using QuantLib in Java/Scala/C# or F# applications via the SWIG layer violates the multi-threading requirement because the garbage collector runs in a different thread and therefore QuantLib objects are shared among different threads. This creates problems with QuantLib’s implementation of the observer pattern and is discussed in detail here.
An improved implementation of the observer pattern based on boost::signals2 together with Riccardo’s thread-safe singleton patch and the multi-threading test runner can obtained from github. Under Linux/MacOS use
./configure --enable-thread-safe-observer-pattern
to enable the thread-safe singleton and the thread-safe observer pattern. Under Windows the corresponding preprocessor directives
#define QL_ENABLE_THREAD_SAFE_OBSERVER_PATTERN
are already set in the file userconfig.hpp. In order to enable the boost shared_ptr hook change the preprocessor variable BOOST_SP_ENABLE_DEBUG_HOOKS towards BOOST_SP_ENABLE_DEBUG_HOOKS_2 in the file
boost/smart_ptr/detail/sp_counted_impl.hpp
Background: the original preprocessor variable BOOST_SP_ENABLE_DEBUG_HOOKS changes the memory layout of the class boost::shared_ptr which might lead to problems with other pre-compiled libraries which also use boost::shared_ptr.
The reward for this work is a stable SWIG interface for Java/Scala/C#/F# and a thread local singleton implementation, which allows to use QuantLib within multi-threading applications as long as SWIG/QuantLib objects aren’t explicitly shared among different threads.