Skip to content

Commit cebc3d1

Browse files
committed
Add boost shared memory example.
1 parent 8ee46ad commit cebc3d1

6 files changed

Lines changed: 538 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31624.102
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SharedMemory1", "SharedMemory1\SharedMemory1.vcxproj", "{928CC868-6C4D-4639-BA47-2346042E3219}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{928CC868-6C4D-4639-BA47-2346042E3219}.Debug|x64.ActiveCfg = Debug|x64
17+
{928CC868-6C4D-4639-BA47-2346042E3219}.Debug|x64.Build.0 = Debug|x64
18+
{928CC868-6C4D-4639-BA47-2346042E3219}.Debug|x86.ActiveCfg = Debug|Win32
19+
{928CC868-6C4D-4639-BA47-2346042E3219}.Debug|x86.Build.0 = Debug|Win32
20+
{928CC868-6C4D-4639-BA47-2346042E3219}.Release|x64.ActiveCfg = Release|x64
21+
{928CC868-6C4D-4639-BA47-2346042E3219}.Release|x64.Build.0 = Release|x64
22+
{928CC868-6C4D-4639-BA47-2346042E3219}.Release|x86.ActiveCfg = Release|Win32
23+
{928CC868-6C4D-4639-BA47-2346042E3219}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {1D1AE6E6-CA24-41FE-A3B2-507C653A3632}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SharedMemory1.cpp : This file contains the 'main' function. Program execution begins and ends there.
2+
//
3+
4+
#include <iostream>
5+
//#include <boost/interprocess/managed_shared_memory.hpp>
6+
#include <boost/interprocess/shared_memory_object.hpp>
7+
#include <boost/interprocess/managed_windows_shared_memory.hpp>
8+
//#include <boost/interprocess/sync/interprocess_semaphore.hpp>
9+
#include <boost/interprocess/sync/named_semaphore.hpp>
10+
#include <boost/interprocess/containers/map.hpp>
11+
#include <boost/interprocess/containers/vector.hpp>
12+
#include <boost/interprocess/allocators/allocator.hpp>
13+
14+
using namespace boost::interprocess;
15+
16+
typedef allocator<uint8_t, managed_windows_shared_memory::segment_manager> ShmemAllocatorV;
17+
typedef vector<uint8_t, ShmemAllocatorV> MyVector;
18+
19+
20+
21+
typedef allocator<char, managed_windows_shared_memory::segment_manager> CharAllocator;
22+
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> MyString;
23+
24+
25+
26+
typedef MyString KeyType;
27+
typedef MyVector MappedType;
28+
typedef std::pair<const MyString, MyVector> ValueType;
29+
typedef allocator<ValueType, managed_windows_shared_memory::segment_manager> ShmemAllocatorMap;
30+
typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocatorMap> MyMap;
31+
32+
33+
34+
struct __Named
35+
{
36+
static inline const char* shared_memory_name = "MySharedMemory";
37+
static inline const char* image_map_name = "MyMap";
38+
static inline const char* image_vec_name = "MyVec";
39+
static inline const char* image_string_name = "MyString";
40+
};
41+
42+
43+
44+
int main()
45+
{
46+
boost::interprocess::named_semaphore sem(open_or_create, "sem", 0);
47+
48+
struct shm_remove
49+
{
50+
//shm_remove() { shared_memory_object::remove(__Named::shared_memory_name); }
51+
~shm_remove() { shared_memory_object::remove(__Named::shared_memory_name); }
52+
} remover;
53+
54+
try
55+
{
56+
std::string id = "abcdefg";
57+
std::vector<uint8_t> image;
58+
59+
60+
for (int i = 0; i < 10; ++i)
61+
{
62+
image.push_back(i);
63+
}
64+
65+
managed_windows_shared_memory segment(create_only, __Named::shared_memory_name, 4096);
66+
67+
68+
MyVector* myvector = segment.construct<MyVector>(__Named::image_vec_name)(image.begin(), image.end(), segment.get_segment_manager());
69+
70+
71+
MyString* mystring = segment.construct<MyString >(__Named::image_string_name)(id, segment.get_segment_manager());
72+
73+
MyMap* mymap = segment.construct<MyMap >(__Named::image_map_name)(segment.get_segment_manager());
74+
mymap->insert(std::make_pair(*mystring, *myvector));
75+
76+
sem.post();
77+
78+
std::cout << "exit!" << std::endl;
79+
80+
}
81+
catch (const interprocess_exception& err)
82+
{
83+
std::cout << err.get_error_code() << ", " << err.what() << std::endl;
84+
}
85+
}
86+
87+
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
88+
// Debug program: F5 or Debug > Start Debugging menu
89+
90+
// Tips for Getting Started:
91+
// 1. Use the Solution Explorer window to add/manage files
92+
// 2. Use the Team Explorer window to connect to source control
93+
// 3. Use the Output window to see build output and other messages
94+
// 4. Use the Error List window to view errors
95+
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
96+
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{928cc868-6c4d-4639-ba47-2346042e3219}</ProjectGuid>
25+
<RootNamespace>SharedMemory1</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
77+
<LinkIncremental>false</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
80+
<LinkIncremental>true</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
86+
<VcpkgTriplet>x64-windows-mixed</VcpkgTriplet>
87+
</PropertyGroup>
88+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
89+
<ClCompile>
90+
<WarningLevel>Level3</WarningLevel>
91+
<SDLCheck>true</SDLCheck>
92+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<ConformanceMode>true</ConformanceMode>
94+
</ClCompile>
95+
<Link>
96+
<SubSystem>Console</SubSystem>
97+
<GenerateDebugInformation>true</GenerateDebugInformation>
98+
</Link>
99+
</ItemDefinitionGroup>
100+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
101+
<ClCompile>
102+
<WarningLevel>Level3</WarningLevel>
103+
<FunctionLevelLinking>true</FunctionLevelLinking>
104+
<IntrinsicFunctions>true</IntrinsicFunctions>
105+
<SDLCheck>true</SDLCheck>
106+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107+
<ConformanceMode>true</ConformanceMode>
108+
</ClCompile>
109+
<Link>
110+
<SubSystem>Console</SubSystem>
111+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
112+
<OptimizeReferences>true</OptimizeReferences>
113+
<GenerateDebugInformation>true</GenerateDebugInformation>
114+
</Link>
115+
</ItemDefinitionGroup>
116+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
117+
<ClCompile>
118+
<WarningLevel>Level3</WarningLevel>
119+
<SDLCheck>true</SDLCheck>
120+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121+
<ConformanceMode>true</ConformanceMode>
122+
<LanguageStandard_C>stdc17</LanguageStandard_C>
123+
<LanguageStandard>stdcpp17</LanguageStandard>
124+
</ClCompile>
125+
<Link>
126+
<SubSystem>Console</SubSystem>
127+
<GenerateDebugInformation>true</GenerateDebugInformation>
128+
</Link>
129+
</ItemDefinitionGroup>
130+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
131+
<ClCompile>
132+
<WarningLevel>Level3</WarningLevel>
133+
<FunctionLevelLinking>true</FunctionLevelLinking>
134+
<IntrinsicFunctions>true</IntrinsicFunctions>
135+
<SDLCheck>true</SDLCheck>
136+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
137+
<ConformanceMode>true</ConformanceMode>
138+
</ClCompile>
139+
<Link>
140+
<SubSystem>Console</SubSystem>
141+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
142+
<OptimizeReferences>true</OptimizeReferences>
143+
<GenerateDebugInformation>true</GenerateDebugInformation>
144+
</Link>
145+
</ItemDefinitionGroup>
146+
<ItemGroup>
147+
<ClCompile Include="SharedMemory1.cpp" />
148+
</ItemGroup>
149+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
150+
<ImportGroup Label="ExtensionTargets">
151+
</ImportGroup>
152+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31624.102
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SharedMemory2", "SharedMemory2\SharedMemory2.vcxproj", "{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Debug|x64.ActiveCfg = Debug|x64
17+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Debug|x64.Build.0 = Debug|x64
18+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Debug|x86.ActiveCfg = Debug|Win32
19+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Debug|x86.Build.0 = Debug|Win32
20+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Release|x64.ActiveCfg = Release|x64
21+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Release|x64.Build.0 = Release|x64
22+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Release|x86.ActiveCfg = Release|Win32
23+
{C73D77F2-30B0-4BCF-BF88-7E9D96E93053}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {61F4DEB7-EB3E-4DBF-9B66-400B5D0756CA}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SharedMemory2.cpp : This file contains the 'main' function. Program execution begins and ends there.
2+
//
3+
4+
#include <iostream>
5+
#include <boost/interprocess/shared_memory_object.hpp>
6+
#include <boost/interprocess/managed_windows_shared_memory.hpp>
7+
#include <boost/interprocess/sync/named_semaphore.hpp>
8+
#include <boost/interprocess/containers/map.hpp>
9+
#include <boost/interprocess/containers/vector.hpp>
10+
#include <boost/interprocess/allocators/allocator.hpp>
11+
using namespace boost::interprocess;
12+
13+
typedef allocator<uint8_t, managed_windows_shared_memory::segment_manager> ShmemAllocatorV;
14+
typedef vector<uint8_t, ShmemAllocatorV> MyVector;
15+
16+
17+
18+
typedef allocator<char, managed_windows_shared_memory::segment_manager> CharAllocator;
19+
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> MyString;
20+
21+
22+
23+
typedef MyString KeyType;
24+
typedef MyVector MappedType;
25+
typedef std::pair<const MyString, MyVector> ValueType;
26+
typedef allocator<ValueType, managed_windows_shared_memory::segment_manager> ShmemAllocatorMap;
27+
typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocatorMap> MyMap;
28+
29+
30+
struct __Named
31+
{
32+
static inline const char* shared_memory_name = "MySharedMemory";
33+
static inline const char* image_map_name = "MyMap";
34+
static inline const char* image_vec_name = "MyVec";
35+
static inline const char* image_string_name = "MyString";
36+
};
37+
int main()
38+
{
39+
boost::interprocess::named_semaphore sem(open_or_create, "sem", 0);
40+
struct shm_remove
41+
{
42+
~shm_remove() { shared_memory_object::remove(__Named::shared_memory_name); }
43+
44+
} remover;
45+
46+
try
47+
{
48+
49+
sem.wait();
50+
51+
managed_windows_shared_memory segment(open_only, __Named::shared_memory_name);
52+
53+
MyVector* myvector = segment.find<MyVector>(__Named::image_vec_name).first;
54+
55+
56+
std::vector<uint8_t> vec;
57+
std::copy(myvector->begin(), myvector->end(), std::back_inserter(vec));
58+
59+
segment.destroy<MyVector>(__Named::image_map_name);
60+
61+
}
62+
catch (const interprocess_exception& err)
63+
{
64+
std::cout << err.get_error_code() << ", " << err.what() << std::endl;
65+
}
66+
}
67+
68+
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
69+
// Debug program: F5 or Debug > Start Debugging menu
70+
71+
// Tips for Getting Started:
72+
// 1. Use the Solution Explorer window to add/manage files
73+
// 2. Use the Team Explorer window to connect to source control
74+
// 3. Use the Output window to see build output and other messages
75+
// 4. Use the Error List window to view errors
76+
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
77+
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

0 commit comments

Comments
 (0)