Commit c71db9db for tesseract
commit c71db9db490a9284ebbf8c48dfa73cac93eacb2b
Author: Stefan Weil <sw@weilnetz.de>
Date: Thu Jun 18 18:07:50 2026 +0200
Use std::call_once for thread-safe initialization in InitIntegerFX
Replace manual mutex + flag pattern with std::call_once for
thread-safe one-time initialization of cos_table and sin_table.
This is the idiomatic C++11 way to ensure a function is executed
exactly once, even when called concurrently from multiple threads.
Assisted-by: minimax-m2.7 (MiniMax)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/classify/intfx.cpp b/src/classify/intfx.cpp
index 77e6153f..0592d129 100644
--- a/src/classify/intfx.cpp
+++ b/src/classify/intfx.cpp
@@ -42,7 +42,7 @@ namespace tesseract {
Global Data Definitions and Declarations
----------------------------------------------------------------------------**/
// Look up table for cos and sin to turn the intfx feature angle to a vector.
-// Protected by atan_table_mutex.
+// Initialized once via std::call_once in InitIntegerFX.
// The entries are in binary degrees where a full circle is 256 binary degrees.
static float cos_table[INT_CHAR_NORM_RANGE];
static float sin_table[INT_CHAR_NORM_RANGE];
@@ -52,17 +52,13 @@ static float sin_table[INT_CHAR_NORM_RANGE];
----------------------------------------------------------------------------**/
void InitIntegerFX() {
- // Guards write access to AtanTable so we don't create it more than once.
- static std::mutex atan_table_mutex;
- static bool atan_table_init = false;
- std::lock_guard<std::mutex> guard(atan_table_mutex);
- if (!atan_table_init) {
+ static std::once_flag flag;
+ std::call_once(flag, []() {
for (int i = 0; i < INT_CHAR_NORM_RANGE; ++i) {
cos_table[i] = cos(i * 2 * M_PI / INT_CHAR_NORM_RANGE + M_PI);
sin_table[i] = sin(i * 2 * M_PI / INT_CHAR_NORM_RANGE + M_PI);
}
- atan_table_init = true;
- }
+ });
}
// Returns a vector representing the direction of a feature with the given