我有一个常见的getter方法,使用rdlock和unlock:
e_state get_state ( )
{
rdlock();
e_state state= m_e_state;
unlock();
return state;
}
带有writelock和解锁的setter:
void set_state (e_state a_new_e_state)
{
wrlock();
m_e_state = a_new_e_state;
unlock();
}
rdlock(),wrlock()和unlock()方法如下:
bool rdlock ( ){return pthread_rwlock_rdlock (&m_rwlock) == 0;}
bool wrlock ( ){return pthread_rwlock_wrlock (&m_rwlock) == 0;}
bool unlock ( ){return pthread_rwlock_unlock (&m_rwlock) == 0;}
使用Valgrind,我收到以下错误:
Conflicting load by thread 3 at 0x04328fa0 size 4
==16557== at 0x8068712: Sample_Namespace::get_state() (CClassManagerXY.h:94)
==16557== by 0x807FA9B: Sample_Namespace::xy_method(std::string const&, Sample_Namespace::IClassX&, int) (IClassY.cpp:773)
==16557== by 0x48CC8CA: Sources::SourceClassX::run() (SourceClassX.cpp:156)
==16557== by 0x8065BEF: run(void*) (CClassManagerXY.cpp:120)
==16557== by 0x402A31B: ??? (in /usr/lib/valgrind/vgpreload_drd-x86-linux.so)
==16557== by 0x4062B24: start_thread (in /lib/libpthread-2.11.2.so)
==16557== by 0x428446D: clone (in /lib/libc-2.11.2.so)
==16557== Address 0x4328fa0 is at offset 8 from 0x4328f98. Allocation context:
==16557== at 0x4029BD3: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_drd-x86-linux.so)
==16557== by 0x807E9FD: Sample_Namespace::loads(std::string const&, bool) (CClassY.cpp:460)
==16557== by 0x807EE9A: Sample_Namespace::load() (CClassY.cpp:542)
==16557== by 0x807D7D4: Sample_Namespace::init() (CClassY.cpp:171)
==16557== by 0x808D57C: main (main.cpp:203)
==16557== Other segment start (thread 2)
==16557== at 0x4284458: clone (in /lib/libc-2.11.2.so)
==16557== Other segment end (thread 2)
==16557== at 0x42925E6: __lll_unlock_wake_private (in /lib/libc-2.11.2.so)
==16557== by 0x41F45DD: _L_unlock_13308 (in /lib/libc-2.11.2.so)
==16557== by 0x41F4527: buffered_vfprintf (in /lib/libc-2.11.2.so)
==16557== by 0x41EF2FA: vfprintf (in /lib/libc-2.11.2.so)
==16557== by 0x41F9BAE: fprintf (in /lib/libc-2.11.2.so)
==16557== by 0x40395F7: Sample_Namespace::write_trace_record(int, unsigned int, char const*, int, char const*, char const*, ...) (CTrace.cpp:106)
==16557== by 0x8065C84: run_adapter(void*) (CClassManagerXY.cpp:124)
==16557== by 0x402A31B: ??? (in /usr/lib/valgrind/vgpreload_drd-x86-linux.so)
==16557== by 0x4062B24: start_thread (in /lib/libpthread-2.11.2.so)
==16557== by 0x428446D: clone (in /lib/libc-2.11.2.so)
我不知道为什么Data Race可以接近状态变量,我在那里管理了锁。我不太明白“地址是从...偏移8”的含义是什么意思。你有什么提示有什么问题吗?
答案 0 :(得分:0)
谢谢PlasmaHH! 我发现还有一种方法可以使用变量。这是我的错。 但是我不太了解,冲突负载真的很危险吗?不仅冲突商店?