Closed MightTower closed 6 years ago
I think I got it, since auto t = m_targets
only copies the pointer its either the old one or the new one in case that m_targets = new_targets;
is excuted simultaneously in another thread. Is this the point why this is thread safe?
Is this the point why this is thread safe?
This library is not thread safe.
At first, memcpy of the pointer is thread-safe only on some platforms
At second, instructions re-ordering is still possible.
atomic_shared_ptr
from C++20 can help, but currently library is not thread-safe
All write operations to m_targets are protected by the m_targets_write_mutex, therefore it should be thread safe... Can you point me to a function/method where modifying m_targets is not protected by the corresponding mutex?
It's not enough to protect writes since shared_ptr isn't atomic. Some CPUs guarantue atomic reads/writes for pointers, but
Protecting writes to shared_ptr is like protecting writes to int: unprotected reads still can introduce data races.
@sergey-shambir this should solve the issue then, right? https://github.com/Kosta-Github/signals-cpp/commit/15ef8dd2a23ecd693268b99974f571e0eee4993e
Yes, it should
Hello, this is more a question than a de facto issue. Is it really thread safe if this code from signal.hpp
is executed and in another thread (B) for example:
is executed. Could it be a problem that m_targets is not locked in the thread B? Maybe I am overlooking something.
Would be great if someone could explain why this is thread save.
Thanks