Skip to content

Commit bdcbe70

Browse files
committed
fix: add mutex protection for unique ID generation
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. 验证应用程序托盘插件在多线程操作下正常工作
1 parent 9ae0b68 commit bdcbe70

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

plugins/application-tray/util.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -318,6 +318,7 @@ void Util::sendXembedMessage(const xcb_window_t& window, const long& message, co
318318

319319
QString Util::generateUniqueId(const QString &id)
320320
{
321+
QMutexLocker locker(&m_idMutex);
321322
for (int i = 0; i < 100; i++) {
322323
QString newId = id + "-" + QString::number(i);
323324
if (!m_currentIds.contains(newId)) {
@@ -331,6 +332,7 @@ QString Util::generateUniqueId(const QString &id)
331332
}
332333

333334
void Util::removeUniqueId(const QString &id) {
335+
QMutexLocker locker(&m_idMutex);
334336
m_currentIds.remove(id);
335337
}
336338

plugins/application-tray/util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -9,6 +9,7 @@
99
#include <QSharedPointer>
1010
#include <QSet>
1111
#include <QObject>
12+
#include <QMutex>
1213

1314
#include <cstdint>
1415
#include <sys/types.h>
@@ -78,6 +79,7 @@ class Util : public QObject
7879
_XDisplay *m_display;
7980

8081
QSet<QString> m_currentIds;
82+
QMutex m_idMutex;
8183
};
8284

8385
}

0 commit comments

Comments
 (0)