我已經(jīng)實(shí)現(xiàn)了各種旨在用于boost :: interprocess共享內(nèi)存段的類(lèi).他們所有的構(gòu)造函數(shù)都使用allocator< void,segment_manager>引用 – 一些顯式在我編寫(xiě)的定義中(如下面的Foo構(gòu)造函數(shù)),有些只是因?yàn)檫@是boost容器定義所需要的,在boost庫(kù)代碼中我不應(yīng)該改變(如下面的IndexVector).
#include <boost/interprocess/managed_shared_memory.hpp>#include <boost/interprocess/allocators/allocator.hpp>#include <boost/interprocess/containers/vector.hpp>typedef boost::interprocess::managed_shared_memory Segment;typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager;typedef boost::interprocess::allocator< void, SegmentManager > Allocator;typedef size_t Index;typedef boost::interprocess::allocator< Index, SegmentManager > IndexAllocator;typedef boost::interprocess::vector< Index, IndexAllocator > IndexVector;class Foo{ public: Foo( const Allocator & alloc ) : mData( alloc ) {} ~Foo() {} private: IndexVector mData;};
大多數(shù)情況下,這些對(duì)象位于共享內(nèi)存中.但我有時(shí)想在非共享內(nèi)存中創(chuàng)建它們的副本.我的問(wèn)題是:我是否必須定義一個(gè)包含不同成員類(lèi)型的整個(gè)不同的類(lèi)(例如Foo_Nonshared)(std :: vector< Index>而不是我的共享IndexVector類(lèi)型)并在它們之間提供復(fù)制/轉(zhuǎn)換功能?這將是很多工作和很多愚蠢的重復(fù).我可以通過(guò)為現(xiàn)有的Foo類(lèi)提供替代構(gòu)造函數(shù)來(lái)減少重復(fù),但后來(lái)我不知道如何在沒(méi)有分配器的情況下初始化IndexVector成員.
還是有一些不錯(cuò)的捷徑?我正在想象我可以傳遞給Foo()的某種特定的分配器實(shí)例,因此它將傳遞給IndexVector構(gòu)造函數(shù),它將被兩者識(shí)別為“在非共享內(nèi)存中分配”.這樣的事情存在嗎?是否存在用于管理香草非共享內(nèi)存的“虛擬段管理器”?或者還有其他解決這個(gè)問(wèn)題的方法嗎?
我希望能夠獲得C 03兼容的答案,盡管我也有興趣學(xué)習(xí)C 11的做法.
更新以下問(wèn)題被標(biāo)記為重復(fù):我已閱讀以前類(lèi)似的問(wèn)題:
> boost::interprocess Containers of containers NOT in shared memory
> boost::interprocess Containers of containers NOT in shared memory copy
并試圖概括我在那里看到的東西,有一些成功和一些失敗(見(jiàn)下面的清單).有一些我無(wú)法解決的編譯器錯(cuò)誤,標(biāo)記為ERROR-特別是我無(wú)法弄清楚如何實(shí)例化迭代這些高度“元”容器成員的方法.但無(wú)論有沒(méi)有這些錯(cuò)誤,我還不知道如何將模板模板制作成可維護(hù)的解決方案(實(shí)際上,我的對(duì)象包含其他復(fù)雜對(duì)象的容器,其中包含更多容器,AFAICS使語(yǔ)法復(fù)雜化,超越了理智. ..看到標(biāo)有“嗯”的部分).
我想,最后,我可能不得不重新設(shè)計(jì),以避免在共享和堆內(nèi)存中使用相同的對(duì)象.
#include <boost/interprocess/managed_shared_memory.hpp>#include <boost/interprocess/allocators/allocator.hpp>#include <boost/interprocess/containers/vector.hpp>namespace bip = boost::interprocess; // warning: C 11 alias declarationtemplate <typename T, template<typename...> class Allocator> // warning: C 11 variadic template using Vector = bip::vector< T, Allocator<T>>; // warning: C 11 alias declaration// this seems to work to get some of the nested <>ness under control.// But I can't figure out how to create an iterator to this kind of type (see errors below)// what once were classes are now class templatestemplate <template<typename...> class Allocator> // warning: C 11 variadic template class Bar { public: Bar( const Allocator<void> & alloc ) : mInts( alloc ) {} ~Bar() {} void Report( void ); private: Vector< int, Allocator > mInts; };template <template<typename...> class Allocator> // warning: C 11 variadic template class Foo { public: Foo( const Allocator<void> & alloc ) : mBars( alloc ) {} ~Foo() {} void Report( void ); private: Vector< Bar<Allocator>, Allocator > mBars; // hmm, with more complex structures this is going // to get unmanageably< nested< very< quickly > > > ... };// Define allocator templatestemplate <typename T> using HeapAllocator = std::allocator<T>; // warning: C 11 alias declarationtemplate <typename T> using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>; // warning: C 11 alias declaration// Define two class variants: one for use on the heap and one for use in shared memoryusing HeapFoo = Foo< HeapAllocator >; // warning: C 11 alias declarationusing ShmemFoo = Foo< ShmemAllocator >; // warning: C 11 alias declaration// Try to define methods (unsuccessful so far because of the iterators,// but they compile OK if the function bodies are left empty):template <template<typename...> class Allocator> // warning: C 11 variadic template void Bar< Allocator >::Report( void ) { std::cout << "["; Vector< int, Allocator >::iterator it;// ERROR: ^~~~~ expected ';' after expression for( it = mInts.begin(); it = mInts.end(); it ) std::cout << ( it == mInts.begin() ? "" : ", " ) << *it; std::cout << "]\n"; }template <template<typename...> class Allocator> // warning: C 11 variadic template void Foo< Allocator >::Report( void ) { Vector< Bar< Allocator >, Allocator >::iterator it;// ERROR: ^~~~~ expected ';' after expression for( it = mBars.begin(); it = mBars.end(); it ) it->Report(); std::cout << "\n"; }int main( void ){ struct shm_remove { shm_remove() { bip::shared_memory_object::remove( "MySharedMemory" ); } ~shm_remove() { bip::shared_memory_object::remove( "MySharedMemory" ); } } remover; bip::managed_shared_memory seg( bip::create_only, "MySharedMemory", 65536 ); ShmemAllocator< void > shalloc( seg.get_segment_manager() ); HeapAllocator< void > halloc; HeapFoo foo1( halloc ); ShmemFoo foo2( shalloc ); foo1.Report(); foo2.Report(); }
解決方法:
好吧,你已經(jīng)遇到了頻繁煩人的邊緣情況,模板模板參數(shù)不是C中的一等公民(你不能傳遞它們/ typedef它們):
我們?cè)撛趺崔k?
> allocator :: rebind< T>
分配器具有重新綁定機(jī)制,我敢說(shuō)正是因?yàn)檫@個(gè)原因.所以你可以傳遞一個(gè)alloc< void>好像它是打開(kāi)的模板,因?yàn)槟憧偸强梢酝ㄟ^(guò)Alloc :: rebind< T> :: other從那里獲得兄弟分配器類(lèi)型.
>除此之外,分配器通常具有執(zhí)行此重新綁定的轉(zhuǎn)換構(gòu)造器這一事實(shí),您不需要在分配器的許多地方過(guò)于具體
>在c 11中,引入了scoped_allocators以避免必須在多個(gè)將執(zhí)行元素內(nèi)部構(gòu)造的地方手動(dòng)傳遞分配器實(shí)例(例如emplace_back).
有適當(dāng)?shù)膸?kù)魔法,會(huì)自動(dòng)將容器的scoped_allocator中的allocator實(shí)例添加為最后一個(gè)構(gòu)造函數(shù)參數(shù)(默認(rèn)情況下). Boost Container庫(kù)已將scoped_allocator_adaptor概念向后移植到c 03,因此您可以使用它.
這是一個(gè)完整的示例,向您展示如何解決您遇到的問(wèn)題,以及如何將基于堆的Bar實(shí)例與共享內(nèi)存Foo實(shí)例混合:
foo2.add(bar1); // this works because of ... MAGIC!
由于上面提到的scoped_allocator,它的工作原理.
#include <boost/interprocess/managed_shared_memory.hpp>#include <boost/interprocess/allocators/allocator.hpp>#include <boost/interprocess/containers/vector.hpp>#include <boost/container/scoped_allocator.hpp>namespace bip = boost::interprocess;namespace generic { template <typename T, typename Alloc/* = std::allocator<T>*/ > using vector = bip::vector<T, typename Alloc::template rebind<T>::other >; template <typename Alloc> struct Bar { typedef Alloc allocator_type; // ties in with uses_allocator/scoped_allocator // only require allocator if not default-constructible Bar(Alloc alloc = Alloc()) : mInts(alloc) {} // conversion constructor so we can convert between allocators template <typename OtherAlloc> Bar(Bar<OtherAlloc> const& rhs, Alloc alloc = Alloc()) : mInts(rhs.mInts.begin(), rhs.mInts.end(), alloc) { } void Report() const; void add(int i) { mInts.emplace_back(i); } private: template<typename OtherAlloc> friend struct Bar; // we can see each other's mInts typedef vector<int, Alloc> ints_t; ints_t mInts; }; template <typename Alloc> struct Foo { typedef Alloc allocator_type; // ties in with uses_allocator/scoped_allocator Foo(Alloc alloc = Alloc()) : mBars(alloc) {} void Report() const; template <typename Bar> void add(Bar const& bar) { mBars.emplace_back(bar); } private: typedef vector<Bar<Alloc>, Alloc> mbars_t; mbars_t mBars; };}namespace heap { using VAlloc = std::allocator<void>; using Bar = generic::Bar<VAlloc>; using Foo = generic::Foo<VAlloc>;}namespace shared { using VAlloc = boost::container::scoped_allocator_adaptor<bip::allocator<void, bip::managed_shared_memory::segment_manager> >; using Bar = generic::Bar<VAlloc>; using Foo = generic::Foo<VAlloc>;}template <typename Alloc> void generic::Bar<Alloc>::Report() const { std::cout << "["; for (typename ints_t::const_iterator it = mInts.begin(); it != mInts.end(); it ) std::cout << (it == mInts.begin() ? "" : ", ") << *it; std::cout << "]\n";}template <typename Alloc>void generic::Foo<Alloc>::Report() const { for (typename mbars_t::const_iterator it = mBars.begin(); it != mBars.end(); it ) it->Report(); std::cout << "\n";}int main(void) { struct shm_remove { shm_remove() { bip::shared_memory_object::remove("MySharedMemory"); } ~shm_remove() { bip::shared_memory_object::remove("MySharedMemory"); } } remover; /////////////////////////////////// // heap based: std::cout << "Heap based storage: \n"; heap::Foo foo1; heap::Bar bar1; bar1.add(42); bar1.add(2); bar1.add(-99); foo1.add(bar1); foo1.Report(); ///////////////////////////////// std::cout << "Shared memory storage: \n"; bip::managed_shared_memory seg(bip::create_only, "MySharedMemory", 65536); shared::VAlloc shalloc(seg.get_segment_manager()); shared::Foo foo2(shalloc); shared::Bar bar2(shalloc); bar2.add(43); bar2.add(3); bar2.add(-98); foo2.add(bar2); // of course this works foo2.add(bar1); // this works because of ... MAGIC! foo2.Report();}
打?。?/p>
Heap based storage: [42, 2, -99]Shared memory storage: [43, 3, -98][42, 2, -99]
來(lái)源:https://www.icode9.com/content-3-474901.html
聯(lián)系客服