notify_one in "client" code doesn't wake "server" wait up.
- open shared memory segment, created by "server",
- find the object there
- and fill this up with a data
for sync used: mutex and condition variable.
this piece of code is a "client"
namespace bi = boost::interprocess;
//....
try {
bi::managed_shared_memory segment(bi::open_only, shm_name.c_str());
bi::named_mutex mutex{bi::open_only, shm_name.c_str()};
bi::named_condition cond(bi::open_only, shm_name.c_str());
std::pair<unsigned char*, bi::managed_shared_memory::size_type> res
= segment.find<unsigned char> (std::to_string(segment_name).c_str());
if(res.second != chunk_size)
throw std::range_error("wrong shared block size");
state = data_segment_state::file_name_only;
size = file_name.size();
{
bi::scoped_lock<bi::named_mutex> lock { mutex };
std::memcpy(res.first, &state, sizeof(data_segment_state));
std::memcpy(res.first + sizeof(data_segment_state), &size, sizeof(size));
std::memcpy(res.first + sizeof(data_segment_state) + sizeof(size),
file_name.c_str(), file_name.size());
cond.notify_one();
}
//... continue the sharing other parts of the data
}
catch (bi::interprocess_exception& ex) {
LOG(ERROR) << ex.what();
//...
}
"server" piece of code (mutex and condition variable is a class members in server):
while(!done) {
bi::scoped_lock<bi::named_mutex> lock {mutex};
condition.wait(lock, [this]{
data_segment_state dss = *reinterpret_cast<data_segment_state*>(data.get());
return dss != data_segment_state::no_data;
});
//...
}
Aucun commentaire:
Enregistrer un commentaire