#include <iostream>
#include <vector>
#include "oneapi/tbb/parallel_for.h"
// 并行累加函数
void parallel_sum(std::vector<int> &vec, std::atomic<int> &total_sum)
{
// 使用 oneapi::tbb::parallel_for 并行化循环
oneapi::tbb::parallel_for(
oneapi::tbb::blocked_range<size_t>(
0, oneapi::tbb::this_task_arena::max_concurrency()),
[&vec, &total_sum](const oneapi::tbb::blocked_range<size_t> &r)
{
int local_sum = 0;
for (size_t i = r.begin(); i != r.end(); ++i)
{
local_sum += vec[i];
}
// 将局部总和累加到全局
total_sum.fetch_add(local_sum); // 使用原子操作避免竞态条件
});
}
int main()
{
// 创建一个包含 1 到 1000 的向量
std::vector<int> vec(1000);
for (int i = 0; i < 1000; ++i)
{
vec[i] = i + 1;
}
// 使用 std::atomic 进行线程安全的累加
std::atomic<int> total_sum;
total_sum = 0; // 初始化为 0
// 设置 4 个线程的 task_arena
oneapi::tbb::task_arena arena(4);
// 在 task_arena 中执行并行累加任务
arena.execute([&]() { parallel_sum(vec, total_sum); });
// 输出结果
std::cout << "Total sum: " << total_sum << std::endl;
return 0;
}
==26865==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7fb92a63ac in __interceptor_malloc ../../../../../../../../../work-shared/gcc-10.2.0-r0/gcc-10.2.0/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x7fb470fce8 (/lib64/libc.so.6+0x6cce8)
#2 0x7fb47100d8 in dlopen (/lib64/libc.so.6+0x6d0d8)
#3 0x7fb924b838 in __interceptor_dlopen ../../../../../../../../../work-shared/gcc-10.2.0-r0/gcc-10.2.0/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:6042
#4 0x7fb6248ea0 (lib/libtbb.so.12+0x18ea0)
#5 0x7fb62522a4 (lib/libtbb.so.12+0x222a4)
#6 0x7fb6257e18 (lib/libtbb.so.12+0x27e18)
#7 0x7fb62584f8 in tbb::detail::r1::initialize(tbb::detail::d1::task_arena_base&) (lib/libtbb.so.12+0x284f8)
#8 0x405230 in void tbb::detail::d0::atomic_do_once<tbb::detail::d1::task_arena::initialize()::{lambda()#1}>(tbb::detail::d1::task_arena::initialize()::{lambda()#1} const&, std::atomic<tbb::detail::d0::do_once_state>&) (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x405230)
#9 0x4028b4 in main (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x4028b4)
#10 0x7fb46cda9c (/lib64/libc.so.6+0x2aa9c)
#11 0x7fb46cdb94 in __libc_start_main (/lib64/libc.so.6+0x2ab94)
#12 0x402d8c in _start (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x402d8c)
Indirect leak of 137 byte(s) in 1 object(s) allocated from:
#0 0x7fb92a63ac in __interceptor_malloc ../../../../../../../../../work-shared/gcc-10.2.0-r0/gcc-10.2.0/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x7fb470a118 (/lib64/libc.so.6+0x67118)
#2 0x7fb46ec0a4 in asprintf (/lib64/libc.so.6+0x490a4)
#3 0x7fb470fc10 in dlerror (/lib64/libc.so.6+0x6cc10)
#4 0x7fb62436b4 (lib/libtbb.so.12+0x136b4)
#5 0x7fb62522c8 (lib/libtbb.so.12+0x222c8)
#6 0x7fb6257e18 (lib/libtbb.so.12+0x27e18)
#7 0x7fb62584f8 in tbb::detail::r1::initialize(tbb::detail::d1::task_arena_base&) (lib/libtbb.so.12+0x284f8)
#8 0x405230 in void tbb::detail::d0::atomic_do_once<tbb::detail::d1::task_arena::initialize()::{lambda()#1}>(tbb::detail::d1::task_arena::initialize()::{lambda()#1} const&, std::atomic<tbb::detail::d0::do_once_state>&) (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x405230)
#9 0x4028b4 in main (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x4028b4)
#10 0x7fb46cda9c (/lib64/libc.so.6+0x2aa9c)
#11 0x7fb46cdb94 in __libc_start_main (/lib64/libc.so.6+0x2ab94)
#12 0x402d8c in _start (/mnt/data/scanner/viscanner-barcode-test/release/arm64-v8a/bin/test_tbb+0x402d8c)
SUMMARY: AddressSanitizer: 161 byte(s) leaked in 2 allocation(s).
运行上面的demo,开启多线程时,c++ Address Sanitizer检测到内存泄露;有知道怎么解决这个问题的不?同样的编译方法我在nxp平台是可以正常运行的,感觉和rk3588的架构有关系呀;
求大佬解答!!!!!!!!!
更多回帖