Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
interprocess/example/doc_shared_ptr.cpp at develop · aleden/interprocess · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
aleden
/
interprocess
Public
forked from
boostorg/interprocess
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
interprocess
/
example
/
doc_shared_ptr.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
144 lines (121 loc) · 4.84 KB
develop
Breadcrumbs
interprocess
/
example
/
doc_shared_ptr.cpp
Copy path
Top
File metadata and controls
Code
Blame
144 lines (121 loc) · 4.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
////////////////////////////////////////////////////////////////////////////
//
//
(C) Copyright Ion Gaztanaga 2006-2012.
//
Distributed under the Boost Software License, Version 1.0.
//
(See accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
//
//
See http://www.boost.org/libs/interprocess for documentation.
//
//
////////////////////////////////////////////////////////////////////////////
#
include
<
boost/interprocess/detail/workaround.hpp
>
//
[doc_shared_ptr
#
include
<
boost/interprocess/managed_mapped_file.hpp
>
#
include
<
boost/interprocess/smart_ptr/shared_ptr.hpp
>
#
include
<
boost/interprocess/smart_ptr/weak_ptr.hpp
>
#
include
<
cassert
>
//
<-
#
include
"
../test/get_process_id_name.hpp
"
//
->
using
namespace
boost
::interprocess
;
//
This is type of the object we want to share
struct
type_to_share
{};
//
This is the type of a shared pointer to the previous type
//
that will be built in the mapped file
typedef
managed_shared_ptr<type_to_share, managed_mapped_file>::type shared_ptr_type;
typedef
managed_weak_ptr<type_to_share, managed_mapped_file>::type weak_ptr_type;
//
This is a type holding a shared pointer
struct
shared_ptr_owner
{
shared_ptr_owner
(
const
shared_ptr_type &other_shared_ptr)
: shared_ptr_(other_shared_ptr)
{}
shared_ptr_owner
(
const
shared_ptr_owner &other_owner)
: shared_ptr_(other_owner.shared_ptr_)
{}
shared_ptr_type shared_ptr_;
//
...
};
int
main
()
{
//
Define file names
//
<-
#
if
1
std::string
mapped_file
(
boost::interprocess::ipcdetail::get_temporary_path
());
mapped_file +=
"
/
"
; mapped_file +=
test::get_process_id_name
();
const
char
*MappedFile = mapped_file.
c_str
();
#
else
//->
const char *MappedFile = "MyMappedFile";
//<-
#
endif
//
->
//
Destroy any previous file with the name to be used.
struct
file_remove
{
file_remove
(
const
char
*MappedFile)
: MappedFile_(MappedFile) {
file_mapping::remove
(MappedFile_); }
~file_remove
(){
file_mapping::remove
(MappedFile_); }
const
char
*MappedFile_;
}
remover
(MappedFile);
{
managed_mapped_file
file
(create_only, MappedFile,
65536
);
//
Construct the shared type in the file and
//
pass ownership to this local shared pointer
shared_ptr_type local_shared_ptr = make_managed_shared_ptr
(file.
construct
<type_to_share>(
"
object to share
"
)(), file);
assert
(local_shared_ptr.
use_count
() ==
1
);
//
Share ownership of the object between local_shared_ptr and a new "owner1"
shared_ptr_owner *owner1 =
file.
construct
<shared_ptr_owner>(
"
owner1
"
)(local_shared_ptr);
assert
(local_shared_ptr.
use_count
() ==
2
);
//
local_shared_ptr releases object ownership
local_shared_ptr.
reset
();
assert
(local_shared_ptr.
use_count
() ==
0
);
assert
(owner1->
shared_ptr_
.
use_count
() ==
1
);
//
Share ownership of the object between "owner1" and a new "owner2"
shared_ptr_owner *owner2 =
file.
construct
<shared_ptr_owner>(
"
owner2
"
)(*owner1);
assert
(owner1->
shared_ptr_
.
use_count
() ==
2
);
assert
(owner2->
shared_ptr_
.
use_count
() ==
2
);
assert
(owner1->
shared_ptr_
.
get
() == owner2->
shared_ptr_
.
get
());
//
<-
(
void
)owner2;
//
->
//
The mapped file is unmapped here. Objects have been flushed to disk
}
{
//
Reopen the mapped file and find again all owners
managed_mapped_file
file
(open_only, MappedFile);
shared_ptr_owner *owner1 = file.
find
<shared_ptr_owner>(
"
owner1
"
).
first
;
shared_ptr_owner *owner2 = file.
find
<shared_ptr_owner>(
"
owner2
"
).
first
;
assert
(owner1 && owner2);
//
Check everything is as expected
assert
(file.
find
<type_to_share>(
"
object to share
"
).
first
!=
0
);
assert
(owner1->
shared_ptr_
.
use_count
() ==
2
);
assert
(owner2->
shared_ptr_
.
use_count
() ==
2
);
assert
(owner1->
shared_ptr_
.
get
() == owner2->
shared_ptr_
.
get
());
//
Now destroy one of the owners, the reference count drops.
file.
destroy_ptr
(owner1);
assert
(owner2->
shared_ptr_
.
use_count
() ==
1
);
//
Create a weak pointer
weak_ptr_type
local_observer1
(owner2->
shared_ptr_
);
assert
(local_observer1.
use_count
() == owner2->
shared_ptr_
.
use_count
());
{
//
Create a local shared pointer from the weak pointer
shared_ptr_type local_shared_ptr = local_observer1.
lock
();
assert
(local_observer1.
use_count
() == owner2->
shared_ptr_
.
use_count
());
assert
(local_observer1.
use_count
() ==
2
);
}
//
Now destroy the remaining owner. "object to share" will be destroyed
file.
destroy_ptr
(owner2);
assert
(file.
find
<type_to_share>(
"
object to share
"
).
first
==
0
);
//
Test observer
assert
(local_observer1.
expired
());
assert
(local_observer1.
use_count
() ==
0
);
//
The reference count will be deallocated when all weak pointers
//
disappear. After that, the file is unmapped.
}
return
0
;
}
//
]
You can’t perform that action at this time.