Commit dab46c0
fix: add thread safety for pinyin dictionary initialization
Add mutex protection to prevent race condition in multi-threaded access
to the static pinyin dictionary. The `InitDict()` function was not
thread-safe when called concurrently from multiple threads, which could
lead to double initialization or data corruption.
1. Add `QMutex` and `QMutexLocker` includes
2. Declare static `dictMutex` to protect the dictionary
3. Lock mutex at the beginning of `InitDict()` to ensure exclusive
access
4. Prevent potential crash when multiple threads simultaneously trigger
dictionary loading
Influence:
1. Test concurrent pinyin conversion from multiple threads
2. Verify no crash occurs during high-frequency multi-threaded access
3. Test dictionary initialization under thread contention
4. Verify performance impact of mutex locking in single-threaded
scenarios
5. Test edge cases where InitDict is called simultaneously from
different threads
fix: 添加拼音字典初始化的线程安全保护
添加互斥锁保护以防止多线程访问静态拼音字典时的竞态条件。`InitDict()` 函
数在被多线程并发调用时不是线程安全的,可能导致重复初始化或数据损坏。
1. 添加 `QMutex` 和 `QMutexLocker` 头文件包含
2. 声明静态 `dictMutex` 用于保护字典
3. 在 `InitDict()` 开头加锁以确保独占访问
4. 防止多线程同时触发字典加载时可能导致的崩溃
Influence:
1. 测试多线程并发进行拼音转换的场景
2. 验证高频多线程访问时无崩溃发生
3. 测试线程竞争条件下的字典初始化
4. 验证单线程场景下互斥锁锁定的性能影响
5. 测试不同线程同时调用 InitDict 的边界情况1 parent 1a081d1 commit dab46c0
1 file changed
Lines changed: 16 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | | - | |
11 | | - | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 21 | + | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
49 | 54 | | |
50 | 55 | | |
51 | 56 | | |
| |||
71 | 76 | | |
72 | 77 | | |
73 | 78 | | |
74 | | - | |
| 79 | + | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
| |||
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
92 | | - | |
| 97 | + | |
93 | 98 | | |
94 | 99 | | |
95 | 100 | | |
96 | 101 | | |
97 | | - | |
| 102 | + | |
98 | 103 | | |
99 | 104 | | |
100 | 105 | | |
| |||
0 commit comments