Как правильно удалить общую память в boost :: interprocess?

я использую boost::interprocess::managed_shared_memory для межпроцессного взаимодействия. Для увеличения / уменьшения общая память должна быть отключена в каждом процессе, использующем ее. Учитывая, что у меня есть две темы, как это сделать?

Насколько я искал boost документации, для этого не существует ничего подобного функции unmap / detach, так что, может быть, мне просто нужно удалить указатель? Будет ли это работать: (не проверено / не скомпилировано)

MyThreadClass::operator() () {
// mutex for thread safe write of Abord/Done fields
std::pair<boost::interprocess::named_mutex*, std::size_t> mutex =
sharedSegment->find<boost::interprocess::named_mutex>("Mutex");
// stop all threads?
std::pair<bool*, std::size_t> abord = sharedSegment->find<bool>("Abord");
// is work of the thread done?
std::pair<bool*, std::size_t> done = sharedSegment->find<bool>("Done");

while(! (*abord.first) ) {
// id == id of thread, if more threads are used
if (! (done.first)[id] ) {
this->doSomethingUseful();
mutex.first->lock();
(done.first)[id] = true;
mutex.first->unlock();
} else {
// when work is done, this thread has nothing to do
//   until the main application tells us so
// by the time we sleep, we can detach the shared memory
// do some busy waiting (change to observer pattern if everything works)
delete mutex;
delete abord;
delete done;
boost::this_thread::sleep(boost::posix_time::seconds(1));
// map variables again
mutex = sharedSegment->find<boost::interprocess::named_mutex>("Mutex");
abord = sharedSegment->find<bool>("Abord");
done = sharedSegment->find<bool>("Done");
}
}

// is thread really finished
std::pair<bool*, std::size_t> killed = sharedSegment->find<bool>("Killed");
mutex.first->lock();
(killed.first)[id] = true;
mutex.first->unlock();
}

MainApplication::someFunction() {
done = sharedSegment->construct<bool>("Done")[noThreads](false);
killed = sharedSegment->construct<bool>("Killed")[noThreads](false);

for (unsigned int i = 0; i < noThreads; ++i) {
// construct new threads with some parameters
new boost::thread(MyThreadClass(...));
}

while ( someCondition ){
// set every thread into working mode
mutex->lock();
for (unsigned int j = 0; j < noThreads; ++j) done[j] = false;
mutex->unlock();

// wait until every thread has finished (busy waiting again)
blockUntilThreadsFinish();

// every thread finished computation here
// change size of shared memory here
// since busy waiting is used, the shared memory will be mapped
//   every second ==> grow/shrink can fail!
// if observer pattern is used, this should work, since the thread
//   sleeps as long, as nothing is observed
}

mutex->lock();
(*abord) = true;
mutex->unlock();
}

Я также считаю, что мне нужно использовать какой-то шаблон наблюдателя, чтобы сделать этот 100-процентный поток безопасным (см. Комментарии в исходном коде).

0

Решение

Удаление вашего объекта shared_memory приведет к отключению общей памяти. Хотя это действительно не повлияет на ваш процесс с точки зрения ресурсов.

0

Другие решения

Других решений пока нет …