英文:
Is Compact Language Detector 2's detect method thread safe?
问题
我们正在使用紧凑语言检测器2的Java封装实现。
detect()函数是线程安全的吗?
据我所了解,它调用了此库函数。
英文:
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.
答案1
得分: 0
不,如果使用CLD2_DYNAMIC_MODE
编译原生代码,则不是线程安全的,您可以使用函数isDataDynamic()
进行测试。
原生函数操作静态类变量kScoringtables
。如果在编译时定义了CLD2_DYNAMIC_MODE
,则此变量将初始化为一组空表(NULL_TABLES
),并且稍后可以使用动态数据加载或由其他线程卸载。
在第1762行的空指针检查处,kScoringtables.quadgram_obj
可能是非空的,然后在第1777行将kScoringtables
的地址更改,然后将其添加到跨线程的ScoringContext
对象中。在这种情况下,错误的指针将在第1785行传递给ApplyHints
,可能导致在第1606行发生不良情况。
这将是一种非常罕见的竞态条件,但仍然有可能发生,并且出于与标准“惰性获取器”不安全的原因,它不是线程安全的。
要使其线程安全,您要么必须测试isDataDynamic()
返回false,要么确保在执行此方法时(或至少直到过了第1777行)不能由不同的线程调用loadDataFromFile
、loadDataFromRawAddress
和unloadData
函数。
英文:
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...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论