...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Boost.Interprocess uses the Windows COM
library to implement some features and initializes it with concurrency
model COINIT_APARTMENTTHREADED
.
If the COM library was already initialized by the calling thread for another
concurrency model, Boost.Interprocess
handles this gracefully and uses COM calls for the already initialized
model. If for some reason, you want Boost.Interprocess
to initialize the COM library with another model, define the macro BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL
before including Boost.Interprocess to
one of these values:
COINIT_APARTMENTTHREADED_BIPC
COINIT_MULTITHREADED_BIPC
COINIT_DISABLE_OLE1DDE_BIPC
COINIT_SPEED_OVER_MEMORY_BIPC
Shared memory (shared_memory_object
)
is implemented in Windows using memory mapped files, placed in a shared
directory in the shared documents folder (SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common AppData
).
This directory name is the last bootup time obtained via Registry values
of the Session Manager. This behaviour is the default since Boost 1.74.
Old Boost.Interprocess versions (until
Boost 1.48 ) used COM calls (via macro BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
)
but that timestamp was unreliable in Virtual Machines and time adjustements.
Next versions until (Boost 1.74) used EventLog startup event as timestamp,
but several users discovered this event could be absent in the Event Log
if the system was up for a long time. You can force this Event Log based
timestamp defining BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED.
In any error case (shared documents folder is not defined or bootup time
could not be obtained), the library throws an error. You still can use
Boost.Interprocess defining your own directory
as the shared directory. When your shared directory is a compile-time constant,
define BOOST_INTERPROCESS_SHARED_DIR_PATH
(and BOOST_INTERPROCESS_SHARED_DIR_WPATH in Windows systems) when using
the library and that path will be used to place shared memory files. When
you have to determine the shared directory at runtime, define BOOST_INTERPROCESS_SHARED_DIR_FUNC
and
implement the following functions
namespace boost { namespace interprocess { namespace ipcdetail { void get_shared_dir(std::string &shared_dir); //wstring overload is only needed for Windows systems void get_shared_dir(std::wstring &shared_dir); } } }
If BOOST_USE_WINDOWS_H
is defined, <windows.h> and other windows SDK files are included,
otherwise the library declares needed functions and structures to reduce
the impact of including those heavy headers.
On systems without POSIX shared memory support, shared memory objects are
implemented as memory mapped files, using a directory placed in "/tmp"
that can include (if BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
is defined) the last bootup time (if the OS supports it). As in Windows,
in any error case obtaining this directory the library throws an error
. When your shared directory is a compile-time constant, define BOOST_INTERPROCESS_SHARED_DIR_PATH
when
using the library and that path will be used to place shared memory files.
When you have to determine the shared directory at runtime, define BOOST_INTERPROCESS_SHARED_DIR_FUNC
and
implement the function
namespace boost { namespace interprocess { namespace ipcdetail { void get_shared_dir(std::string &shared_dir); } } }
The committed address space is the total amount of virtual memory (swap or physical memory/RAM) that the kernel might have to supply if all applications decide to access all of the memory they've requested from the kernel. By default, Linux allows processes to commit more virtual memory than available in the system. If that memory is not accessed, no physical memory + swap is actually used.
The reason for this behaviour is that Linux tries to optimize memory usage on forked processes; fork() creates a full copy of the process space, but with overcommitted memory, in this new forked instance only pages which have been written to actually need to be allocated by the kernel. If applications access more memory than available, then the kernel must free memory in the hard way: the OOM (Out Of Memory)-killer picks some processes to kill in order to recover memory.
Boost.Interprocess has no way to change this behaviour and users might suffer the OOM-killer when accessing shared memory. According to the Kernel documentation, the Linux kernel supports several overcommit modes. If you need non-kill guarantees in your application, you should change this overcommit behaviour.
Starting from FreeBSD 11, declares the macro _POSIX_THREAD_PROCESS_SHARED. However, the default behavior is different from the one on Linux. If you want to use this feature, according to the man page of libthr(3), you should check sysctl's kern.ipc.umtx_vnode_persistent:
If you want mapped files to remain useful after the last handle is closed, set this variable to 1.
Boost.Interprocess uses POSIX shared memory in MacOS to implement shared memory classes in MacOS.
But sandboxed apps cannot use System V (XSI) semaphores and POSIX semaphores and shared memory can't be used by default. However, by specifying an entitlement that requests membership in an application group, an app can use these technologies to communicate with other members of that application group.
Please see Apple documentation for limitations and guide on how to use interprocess communications on sandboxed applications.
Many people have contributed with ideas and revisions, so this is the place to thank them:
unordered_map_index
class.
TimedLockable
requirements and condition variables implement wait_until/wait_for
operations.
boost::date_time::ptime
, std::time_point
or boost::chrono::time_point
.
wchar_t
API support
for named resources in operating systems that offer native wide character
API (e.g. Windows). The following classes were updated with wchar_t
name support:
file_mapping
managed_mapped_file
managed_shared_memory
managed_windows_shared_memory
shared_memory_object
windows_shared_memory_object
file_lock
named_condition
named_condition_any
named_mutex
named_recursive_mutex
named_semaphore
named_sharable_mutex
named_upgradable_mutex
message_queue
BOOST_INTERPROCESS_FORCE_NATIVE_EMULATION
macro option to disable the use of generic emulation code for process
shared synchronization primitives instead of native POSIX or Winapi
functions.
BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
is now the default value. You can obtain the pre-Boost 1.73 behaviour
defining BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
.
The old and broken pre-Boost 1.54 behaviour (BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
)
is no longer available.
BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
from Windows systems. This option derives the unique bootstamp used
to name the folder where shared memory is placed from registry values
associated with the session manager. This option only works on Vista
and later systems and might be more stable than the default version.
offset_ptr
performance and removed any undefined behaviour. No special cases needed
for different compilers.
unique_ptr
,
now forwards boost::interprocess::unique_ptr to the general purpose
boost::movelib::unique_ptr
class from Boost.Move.
This implementation is closer to the standard std::unique_ptr
implementation and it's better maintained.
BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
option of message queue, was completely broken so an ABI break was
necessary to have a working implementation.
BOOST_INTERPROCESS_SHARED_DIR_PATH
option to define the shared directory used to place shared memory objects
when implemented as memory mapped files.
BOOST_USE_WINDOWS_H
.
When this macro is defined Interprocess does not declare used Windows
API function and types, includes all needed windows SDK headers and
uses types and functions declared by the Windows SDK.
get_size
to
::boost:interprocess:windows_shared_memory
.
LastBootupTime
from WMI was unstable with time synchronization and hibernation and
unusable in practice. If you really need to obtain pre Boost 1.54 behaviour
define BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
from command line or detail/workaround.hpp
.
shrink_by
and
advise
functions in
mapped_region
.
message_queue
with a circular buffer
index (the old behavior used an ordered array, leading to excessive
copies). This should greatly increase performance but breaks ABI. Old
behaviour/ABI can be used undefining macro BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
in boost/interprocess/detail/workaround.hpp
message_queue
insertion time avoiding priority search for common cases (both array
and circular buffer configurations).
interproces_sharable_mutex
and interproces_condition_any
.
offset_ptr
performance.
mapped_region::flush
.
get_offset
method from
mapped_region
as it
has no practical utility and m_offset
member was not for anything else.
flush
from managed_shared_memory
. as it is unspecified
according to POSIX: "The
effect of msync() on a shared memory object or a typed memory object
is unspecified" .
#define
BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
in the windows configuration part to get Boost 1.48 & Boost 1.49
behaviour.
mapped_region::flush
initiates disk flushing but
does not guarantee it's completed when returns, since it is not portable.
semaphore
and named_semaphore
now implemented more efficiently with atomic operations.
stable_vector
container.
shared_memory_object::remove
has now POSIX unlink
semantics and file_mapping::remove
was added to obtain POSIX unlink
semantics with mapped files.
BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION
macro option to force the use of generic emulation code for process-shared
synchronization primitives instead of native POSIX functions.
boost::posix_time::pos_inf
value is now handled portably
for timed functions.
iterator
to const_iterator
in
containers to keep up with the draft of the next standard.
void
return
types from flat_map::erase()
functions.
mapped_region
the mode used to create
it.
shared_ptr
is movable
and supports aliasing.
shared_ptr
,
weak_ptr
and unique_ptr
. Added
explanations and examples of these smart pointers in the documentation.
pointer
as an smart pointer. This increases performance and improves
compilation times.
named_mutex
and named_semaphore
with POSIX named semaphores in systems supporting that option. named_condition
has been accordingly changed to support interoperability with named_mutex
.
mapped_region
in UNIX when mapping address was provided but the region was mapped
in another address.
aligned_allocate
and allocate_many
functions
to managed memory segments.
get_instance_name
,
get_instance_length
and get_instance_type
functions to managed memory segments.
rbtree_best_fit
.
vector
.
segment_manager
to avoid code bloat associated with templated instantiations.
deque::clear()
and deque::erase()
, they were declared private.
deque::erase()
. Thanks to Steve LoBasso.
atomic_dec32()
. Thanks to Glenn Schrader.
offset_ptr
pointers.
managed_mapped_file
.
boost::has_trivial_destructor
.
This optimization avoids calling destructors of elements that have
a trivial destructor.
has_trivial_destructor_after_move
trait. This optimization avoids calling destructors of elements that
have a trivial destructor if the element has been moved (which is the
case of many movable types). This trick was provided by Howard Hinnant.
deallocate_free_chunks()
) to manually deallocate completely
free chunks from node allocators.
multiset
and multimap
classes.
shared_memory_object.hpp
header instead of shared
memory.hpp
.
mode_t
type.
mapped_region
constructor no longer requires classes derived from memory_mappable,
but classes must fulfill the MemoryMappable concept.
Some useful references about the C++ programming language, C++ internals, shared memory, allocators and containers used to design Boost.Interprocess.
There are some Interprocess features that I would like to implement and some Boost.Interprocess code that can be much better. Let's see some ideas:
Win32 version of shared mutexes and shared conditions are based on "spin and wait" atomic instructions. This leads to poor performance and does not manage any issues like priority inversions. We would need very serious help from threading experts on this. And I'm not sure that this can be achieved in user-level software. Posix based implementations use PTHREAD_PROCESS_SHARED attribute to place mutexes in shared memory, so there are no such problems. I'm not aware of any implementation that simulates PTHREAD_PROCESS_SHARED attribute for Win32. We should be able to construct these primitives in memory mapped files, so that we can get filesystem persistence just like with POSIX primitives.
Currently Interprocess only allows char based names for basic named objects. However, several operating systems use wchar_t names for resources (mapped files, for example). In the future Interprocess should try to present a portable narrow/wide char interface. To do this, it would be useful to have a boost wstring <-> string conversion utilities to translate resource names (escaping needed characters that can conflict with OS names) in a portable way. It would be interesting also the use of boost::filesystem paths to avoid operating system specific issues.
Boost.Interprocess does not define security attributes for shared memory and synchronization objects. Standard C++ also ignores security attributes with files so adding security attributes would require some serious work.
Boost.Interprocess offers a process-shared message queue based on Boost.Interprocess primitives like mutexes and conditions. I would want to develop more mechanisms, like stream-oriented named fifo so that we can use it with a iostream-interface wrapper (we can imitate Unix pipes).
C++ needs more complex mechanisms and it would be nice to have a stream and datagram oriented PF_UNIX-like mechanism in C++. And for very fast inter-process remote calls Solaris doors is an interesting alternative to implement for C++. But the work to implement PF_UNIX-like sockets and doors would be huge (and it might be difficult in a user-level library). Any network expert volunteer?