fix: add mutex protection for unique ID generation#450
Merged
18202781743 merged 1 commit intolinuxdeepin:masterfrom Apr 22, 2026
Merged
fix: add mutex protection for unique ID generation#45018202781743 merged 1 commit intolinuxdeepin:masterfrom
18202781743 merged 1 commit intolinuxdeepin:masterfrom
Conversation
1. Added QMutexLocker to protect the generateUniqueId method from concurrent access 2. Added QMutexLocker to protect the removeUniqueId method from concurrent access 3. Added m_idMutex member variable to util.h for thread synchronization 4. This fixes potential race conditions when multiple threads call these methods simultaneously, ensuring unique IDs remain unique and preventing data corruption of m_currentIds Influence: 1. Test generating unique IDs from multiple threads simultaneously 2. Verify that no duplicate IDs are produced under concurrent access 3. Test removing IDs while other threads are generating new ones 4. Verify no crashes or data corruption occurs with heavy multi-threaded usage 5. Test with stress conditions (100+ concurrent threads) 6. Verify that the application tray plugin works correctly with multi- threaded operations fix: 添加互斥锁保护唯一ID生成 1. 为 generateUniqueId 方法添加 QMutexLocker 保护并发访问 2. 为 removeUniqueId 方法添加 QMutexLocker 保护并发访问 3. 在 util.h 中添加 m_idMutex 成员变量用于线程同步 4. 修复了多线程同时调用这些方法时的竞态条件问题,确保唯一ID保持唯一性, 防止 m_currentIds 数据损坏 Influence: 1. 测试多线程同时生成唯一ID 2. 验证并发访问下不会产生重复ID 3. 测试在生成新ID的同时移除其他ID的场景 4. 验证高并发使用下不会出现崩溃或数据损坏 5. 测试压力条件(100+并发线程) 6. 验证应用程序托盘插件在多线程操作下正常工作
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds mutex-based synchronization around unique ID generation/removal in the application tray util to make ID management thread-safe, and updates the SPDX copyright years. Sequence diagram for concurrent unique ID generation with mutexsequenceDiagram
actor ThreadA
actor ThreadB
participant UtilInstance as Util
participant Mutex as m_idMutex
ThreadA->>UtilInstance: generateUniqueId(id)
UtilInstance->>Mutex: lock via QMutexLocker
activate UtilInstance
UtilInstance->>UtilInstance: iterate, check m_currentIds, insert newId
ThreadB->>UtilInstance: generateUniqueId(id)
UtilInstance->>Mutex: wait for lock
UtilInstance-->>Mutex: unlock when QMutexLocker destroyed
deactivate UtilInstance
UtilInstance-->>ThreadA: return uniqueIdA
UtilInstance->>Mutex: lock via QMutexLocker
activate UtilInstance
UtilInstance->>UtilInstance: iterate, check m_currentIds, insert newId
UtilInstance-->>Mutex: unlock when QMutexLocker destroyed
deactivate UtilInstance
UtilInstance-->>ThreadB: return uniqueIdB (different from uniqueIdA)
Class diagram for updated Util ID synchronizationclassDiagram
class Util {
+QString generateUniqueId(QString id)
+void removeUniqueId(QString id)
-QSet<QString> m_currentIds
-QMutex m_idMutex
}
class QMutex {
}
class QSet_QString_ {
}
Util "1" --> "1" QMutex : uses m_idMutex
Util "1" --> "1" QSet_QString_ : manages m_currentIds
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review这段代码的修改主要是为了解决多线程访问 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
5. 其他建议
改进后的代码示例// util.h
class Util : public QObject
{
// ... 其他成员 ...
private:
QSet<QString> m_currentIds;
QMutex m_idMutex; // 保护 m_currentIds 的线程安全访问
};
// util.cpp
QString Util::generateUniqueId(const QString &id)
{
QMutexLocker locker(&m_idMutex);
for (int i = 0; i < 100; i++) {
QString newId = id + "-" + QString::number(i);
if (!m_currentIds.contains(newId)) {
m_currentIds.insert(newId);
return newId;
}
}
qWarning() << "Failed to generate unique ID for base ID:" << id;
return QString(); // 返回空字符串表示失败
}
void Util::removeUniqueId(const QString &id) {
QMutexLocker locker(&m_idMutex);
m_currentIds.remove(id);
}总结这段修改解决了线程安全问题,但可以在性能和错误处理方面进一步优化。建议根据实际使用场景评估是否需要调整锁的粒度或 ID 生成策略。 |
mhduiy
approved these changes
Apr 22, 2026
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, mhduiy The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
concurrent access
concurrent access
methods simultaneously, ensuring unique IDs remain unique and preventing
data corruption of m_currentIds
Influence:
usage
threaded operations
fix: 添加互斥锁保护唯一ID生成
防止 m_currentIds 数据损坏
Influence:
Summary by Sourcery
Add thread-safe protection around application tray unique ID management to prevent race conditions under concurrent access.
Bug Fixes:
Enhancements: