c++ - Two threads modifying same object at same time -
in simple server-client mechanism i'm writing, server stores set of key-value mappings in stl map. talking multiple clients, separate thread created each, , i've passed map threads using std::ref (i need common keystore). while works, possible that:
1) if 2 clients communicate @ same time, 2 threads try modify map @ same time?
2) in case, happen?
3) possibly avoid bad?
1) if 2 clients communicate @ same time, 2 threads try modify map @ same time?
yes, try modify @ same time
2) in case, happen?
you have undefined behavior. can happen.
3) possibly avoid bad?
use std::lock_guard
avoid problem. can refer link more details: http://en.cppreference.com/w/cpp/thread/lock_guard
Comments
Post a Comment