gpt4 book ai didi

c++ - DRD 在 std::mutex::lock 上报告 "conflicting load"错误

转载 作者:行者123 更新时间:2023-12-02 02:09:53 25 4
gpt4 key购买 nike

考虑以下代码:

#include <mutex>
#include <thread>
#include <vector>
#include <thread>

struct counter {
counter()
{
}

size_t
get()
{
std::unique_lock<std::mutex> lock(mutex);

return 0;
}

std::mutex mutex;
};

static counter &
get_counter()
{
static counter manager;
return manager;
};

static void do_work()
{
get_counter().get();
}

int main()
{
std::vector<std::thread> v;
for (int i = 0; i < 16; i++)
v.emplace_back([]{ do_work(); });

for (auto &t : v)
t.join();
}

编译时:g++ -std=c++11 source.cpp -lpthread并在valgrind下运行valgrind --tool=drd ./a.out我收到以下错误:

==4297== drd, a thread error detector
==4297== Copyright (C) 2006-2017, and GNU GPL'd, by Bart Van Assche.
==4297== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4297== Command: ./a.out
==4297==
==4297== Thread 3:
==4297== Conflicting load by thread 3 at 0x0010f050 size 4
==4297== at 0x4844083: vgDrd_mutex_type (drd_pthread_intercepts.c:380)
==4297== by 0x4844083: pthread_mutex_lock_intercept (drd_pthread_intercepts.c:885)
==4297== by 0x4844083: pthread_mutex_lock (drd_pthread_intercepts.c:898)
==4297== by 0x109E72: __gthread_mutex_lock(pthread_mutex_t*) (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109EC7: std::mutex::lock() (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x10A4A8: std::unique_lock<std::mutex>::lock() (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x10A206: std::unique_lock<std::mutex>::unique_lock(std::mutex&) (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x10A0C4: counter::get() (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x10940B: do_work() (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109420: main::{lambda()#1}::operator()() const (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109E06: void std::__invoke_impl<void, main::{lambda()#1}>(std::__invoke_other, main::{lambda()#1}&&) (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109DA7: std::__invoke_result<main::{lambda()#1}>::type std::__invoke<main::{lambda()#1}>(std::__invoke_result&&, (main::{lambda()#1}&&)...) (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109D45: void std::thread::_Invoker<std::tuple<main::{lambda()#1}> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== by 0x109D06: std::thread::_Invoker<std::tuple<main::{lambda()#1}> >::operator()() (in /home/igchor/Projects/libpmemobj-cpp/build/a.out)
==4297== Allocation context: BSS section of /home/igchor/Projects/libpmemobj-cpp/build/a.out
==4297== Other segment start (thread 2)
==4297== (thread finished, call stack no longer available)
==4297== Other segment end (thread 2)
==4297== (thread finished, call stack no longer available)
==4297==
==4297==
==4297== For lists of detected and suppressed errors, rerun with: -s
==4297== ERROR SUMMARY: 15 errors from 1 contexts (suppressed: 20 from 12)

这是静态结构初始化方式的问题吗?我认为这是误报,因为静态对象的初始化应该是线程安全的。有什么办法可以消除这个错误吗?

注意:现在,当我删除 counter 结构的构造函数时,它看起来像这样:

struct counter {
size_t
get()
{
std::unique_lock<std::mutex> lock(mutex);

return 0;
}

std::mutex mutex;
};

drd 不报告任何错误。

我在 Ubuntu 19.10 上测试了所有这些:

  • g++-9.2.1(和 clang++ 9.0.0)
  • valgrind 3.15

最佳答案

已确认这是一个 drd bug,并且应该已经在 drd master 上修复: https://bugs.kde.org/show_bug.cgi?id=416286

关于c++ - DRD 在 std::mutex::lock 上报告 "conflicting load"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com