We are using the Java Wrapper implementation of Compact Language Detector 2.
Is the detect() function thread-safe?
From what I understand, it invokes this library function.
No, it is not thread safe if the native code was compiled with CLD2_DYNAMIC_MODE
set, which you could test using the function isDataDynamic()
.
The native function manipulates the static class variable kScoringtables
. If CLD2_DYNAMIC_MODE
is defined at compilation, this variable is initialized to a set of null tables (NULL_TABLES
) and can later be loaded with dynamic data, or unloaded, potentially by other threads.
It would be possible for the kScoringtables.quadgram_obj
to be non-null at the line 1762 null check and then the kScoringtables
address altered before it is added to the cross-thread ScoringContext
object on line 1777. In this case, the wrong pointer would be passed to ApplyHints
on line 1785, potentially causing bad things to happen at line 1606.
This would be a very rare race condition, but possible nonetheless, and is not thread safe for the same reason the standard "lazy getter" is not thread safe.
To make this thread-safe, you would have to either test that isDataDynamic()
returns false, or ensure the loadDataFromFile
, loadDataFromRawAddress
, and unloadData
functions could not be called by a different thread while you are executing this method (or at least until you are past line 1777...)