---
title: 自定义锁过滤使用场景
description: "用户可以通过在内存一致性动态检查命令devkit advisor dr-check后，添加“-cf/--config-file”参数的方式，实现在内存一致性动态检查期间不检查相关的自定义锁。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/development/affinityanalyzer/KunpengDevKitCli_0096.html
sourcePath: /source/zh/kunpengdevps/development/affinityanalyzer/KunpengDevKitCli_0096.html
indexId: 8743e3cd495fc1f156b2aef0b7f5e793de997848e5e2933fc5b8c0bf076d118879
---
# 自定义锁过滤使用场景

用户可以通过在内存一致性动态检查命令devkit advisor dr-check后，添加“-cf/--config-file”参数的方式，实现在内存一致性动态检查期间不检查相关的自定义锁。

添加“-cf/--config-file”参数需指定JSON配置文件，文件中写入需要过滤的自定义实现的加锁和解锁函数。

#### 自定义锁过滤使用示例

本示例通过指定自定义锁过滤配置文件，执行内存一致性动态检查后实现自定义锁过滤操作。

1. 编译源码文件，生成可执行二进制文件。此处以自定义类添加锁为例，请根据实际情况在“-cf”参数指定的配置文件中添加加锁、解锁函数。
  1 g++ test.cpp -o test -g -lpthread

  “test.cpp”文件内容如下： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include <atomic> #include <iostream> #include <pthread.h> class my_custom_spinlock{ std::atomic_flag is_locked ={false}; public: void lock(){ while (is_locked.test_and_set(std::memory_order_acquire)){} } void unlock (){ is_locked.clear (std::memory_order_release); } }; int data_race = 0; my_custom_spinlock spinlock; void* first_thread(void* arg){ spinlock.lock(); data_race = 5; spinlock.unlock(); return 0; } void* second_thread(void* arg){ spinlock.lock(); data_race = 3; spinlock.unlock(); return 0; } int main(int argc, char *argv[]) { pthread_t thr1, thr2; pthread_create(&thr1, NULL, first_thread, NULL); pthread_create(&thr2, NULL, second_thread, NULL); pthread_join(thr1, NULL); pthread_join(thr2, NULL); for(int i = 1; i <argc; ++i){ std::cout<< argv[i] << std::endl; } std::cout<< data_race << std::endl; return 0; }

2. 执行内存一致性动态检查。“/home/advisor/test”为ELF文件路径，请根据实际情况进行修改。

```
devkit advisor dr-check -f
/home/advisor/test
```


  返回信息如下，从返回信息中的加粗信息可以看出有2行代码推荐修改。

```
Executing dynamic memory consistency check task, please wait...
The pid of the memtracer process is 1459201
Scanned time: 2025/05/11 07:06:12
Configuration:
ELF filepath: /home/advisor/test
Scan source code path: /home/testcase/project
Generate report path: /home/DevKit_Software
Generate report type: all
Task Timeout Interval: The timeout period is not set.
Log level: info
Summary:
There are 2 recommended code lines to modify.
For the detailed information on multi-thread races, please check:
/home/DevKit_Software/dr-check_test_20250511070612_124d.json
/home/DevKit_Software/dr-check_test_20250511070612_124d.html
/home/DevKit_Software/dr-check_test_20250511070612_124d.csv
```


3. 对于自定义类锁my_custom_spinlock的过滤，创建配置文件config.json，在配置文件中写入需要过滤的加锁和解锁函数相关内容。
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 { "receive": [ { "id_arg": 0, "name":"my_custom_spinlock::lock(*)", "shared": false, "success": "always" } ], "send": [ { "id_arg": 0, "name": "my_custom_spinlock::unlock(*)" } ] }

  receive列表为自定义实现的加锁函数，表示等待资源的函数（和pthread_mutex_lock原理相似）；send列表为自定义实现的解锁函数，表示释放资源的函数（和pthread_mutex_unlock原理相似）。 加锁和解锁函数共有配置选项字段介绍： id_arg：代表在加锁/解锁函数的参数列表中能够代表锁对象的索引（通常情况下锁对象是一个指针）。 name：代表加锁/解锁函数名全称，C++语言中函数名书写方式为“class::function_name(*)”，C语言中函数名书写方式为“function_name”，可用“*”匹配或跳过任意字符。 加锁函数特有配置选项字段介绍： shared：代表该函数操作的对象是否为读锁。取值范围为false、true，默认值为false；若为读锁，在读写锁上下文中，shared字段值为true。 success：代表函数的返回值为何值时表示加锁成功。取值范围为always、zero、non_zero，默认为always，若自定义实现try_lock类型加锁函数，则根据函数返回值为0（对应zero）或非0（对应non_zero）配置该字段。

4. 再次执行内存一致性动态检查。

```
devkit advisor dr-check -f
/home/advisor/test
-cf
/home/testcase/config.json
```


  “/home/testcase/config.json”：JSON配置文件，指定在内存一致性动态检查期间不检查的自定义锁，请根据实际情况进行修改。

  返回信息如下，从返回信息中的加粗信息可以看出无代码行需要修改，实现自定义锁过滤操作。

```
Executing dynamic memory consistency check task, please wait...
The pid of the memtracer process is 1459201
Scanned time: 2025/05/12 07:08:15
Configuration:
ELF filepath: /home/advisor/test
Scan source code path: /home/testcase/project
Generate report path: /home/DevKit_Software
Generate report type: all
Task Timeout Interval: The timeout period is not set.
Log level: info
Summary:
There are 0 recommended code lines to modify.
For the details information on multi-thread races, please check:
/home/DevKit_Software/dr-check_test_20250512070815_1256.json
/home/DevKit_Software/dr-check_test_20250512070815_1256.html
/home/DevKit_Software/dr-check_test_20250512070815_1256.csv
```
