简体   繁体   English

如何正确地取消对boost :: interprocess中共享内存的映射?

[英]How to correctly unmap shared memory in boost::interprocess?

I'm using boost::interprocess::managed_shared_memory for interprocess communication. 我正在使用boost::interprocess::managed_shared_memory进行进程间通信。 In order to grow/shrink, the shared memory must be unmapped in every process using it. 为了增加/缩小,必须在使用共享内存的每个进程中将其取消映射。 Given I have a two threads, how is this done? 给定我有两个线程,这是怎么做的?

As far as I searched the boost documentation, there does not exist anything like an unmap/detach function for this, so maybe I just have to delete the pointer? 就我搜索boost文档而言,不存在像unmap / detach函数这样的功能,所以也许我只需要删除指针? Would this work: (untested/not compiled) 这项工作是否可行:(未经测试/未编译)

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();
}

I also believe that I have to use some observer pattern to make this 100 percent thread safe (see comments in the source code). 我也相信我必须使用一些观察者模式来确保此100%线程安全(请参见源代码中的注释)。

Delete your shared_memory object will detach the shared memory. 删除您的shared_memory对象将分离共享内存。 Although it really wont impact your process resource-wise. 尽管它确实不会影响您的过程资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM