473,695 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL and shared memory

The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?

Jul 22 '05 #1
11 4891
Użytkownik Michael Schuler napisał, On 2004-01-12 15:01:
The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?

AFAIR to use shared memory you should use boost library you can find it at:
http://www.boost.org/.
Documentation:
http://www.boost.org/libs/smart_ptr/smart_ptr.htm

Best Regards
Darek Ostolski
--
ERROR 1164 HOW IN THE HELL DID YOU GET HERE
Jul 22 '05 #2
Hello,

take a look to <http://tplusplus.sourc eforge.net/> resp. at its source code. It uses special
allocators to place the contents of STL containers into shared memory. It's been presented
at OOPSLA 2003.

Cheers,
Philipp.
"Michael Schuler" <Mi************ *@fujitsu-siemens.com> wrote in message news:bt******** **@news.fujitsu-siemens.com...
The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?

Jul 22 '05 #3
On Mon, 12 Jan 2004 15:01:07 +0100, Michael Schuler
<Mi************ *@fujitsu-siemens.com> wrote:
The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?


If you can allocate your shared memory at a fixed address in each
process' address space, then you just need to write a custom allocator
to allocate from that memory. But if you can't allocate at a fixed
address, then you have to write a complex allocator that uses "offset"
smart pointers (and references!) that store an offset into the shared
memory rather than a pure pointer, and possibly a "shared memory ID"
or similar if you want to have more than one shared memory area in any
process. This is hard to do in a reliable way since C++ doesn't really
support smart references.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
Thanks Tom,

but to my knowledge if I use an allocator, only the items are in shared
memory not the container itself. In my case teh container also must lie
in shared memory to survive a process failure :-(

tom_usenet wrote:
On Mon, 12 Jan 2004 15:01:07 +0100, Michael Schuler
<Mi************ *@fujitsu-siemens.com> wrote:

The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?

If you can allocate your shared memory at a fixed address in each
process' address space, then you just need to write a custom allocator
to allocate from that memory. But if you can't allocate at a fixed
address, then you have to write a complex allocator that uses "offset"
smart pointers (and references!) that store an offset into the shared
memory rather than a pure pointer, and possibly a "shared memory ID"
or similar if you want to have more than one shared memory area in any
process. This is hard to do in a reliable way since C++ doesn't really
support smart references.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Jul 22 '05 #5
Hello,

that's true, but imposes no problem at all, because there's "placement new",
i.e. given we talk about Unix and given there's some magic "shmAllocator<> ",
you can write sth like

...
typedef std::map< int,std::less< int >,shmAllocato r< int > > shmIntMap;
shm=shmget(shm_ key,sizeof(shmI ntMap),IPC_CREA T|0700);
void *const p=(shmIntMap *)shmat(shm,NUL L,0);
shmIntMap *m=new(p) shmIntMap;
...

Then, don't forget to manually call the destructor afterwards.

Now, you might ask, o.k., now both the map and the ints contained within the map
go into the shared memory - but what's about the linked structure that establishes
the tree the map internally consists of, i.e. that glues together the container and
its elements? The answer is the "rebind<>" member template class a conforming
allocator has to have.

Cheers,
Philipp.
but to my knowledge if I use an allocator, only the items are in shared
memory not the container itself. In my case teh container also must lie
in shared memory to survive a process failure :-(
Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?


If you can allocate your shared memory at a fixed address in each
process' address space, then you just need to write a custom allocator
to allocate from that memory. But if you can't allocate at a fixed
address, then you have to write a complex allocator that uses "offset"
smart pointers (and references!) that store an offset into the shared
memory rather than a pure pointer, and possibly a "shared memory ID"
or similar if you want to have more than one shared memory area in any
process. This is hard to do in a reliable way since C++ doesn't really
support smart references.

Jul 22 '05 #6

"void" <chq@nie_spamuj .wp.pl> wrote in message
news:29******** ******@213.17.1 92.132...
Użytkownik Michael Schuler napisał, On 2004-01-12 15:01:
The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?
AFAIR to use shared memory you should use boost library you can find

it at: http://www.boost.org/.
Documentation:
http://www.boost.org/libs/smart_ptr/smart_ptr.htm


Boost has excellent smart pointers, but does not provide any help with
accessing shared memory.

Jonathan
Jul 22 '05 #7
Thanks Philipp, good hint,

but I still have the problem with pointers inside the container,
whereas pointers pose problems since the shared memory may have
different addresses in different processes :-(

Philipp Bachmann
Hello,

that's true, but imposes no problem at all, because there's "placement new",
i.e. given we talk about Unix and given there's some magic "shmAllocator<> ",
you can write sth like

...
typedef std::map< int,std::less< int >,shmAllocato r< int > > shmIntMap;
shm=shmget(shm_ key,sizeof(shmI ntMap),IPC_CREA T|0700);
void *const p=(shmIntMap *)shmat(shm,NUL L,0);
shmIntMap *m=new(p) shmIntMap;
...

Then, don't forget to manually call the destructor afterwards.

Now, you might ask, o.k., now both the map and the ints contained within the map
go into the shared memory - but what's about the linked structure that establishes
the tree the map internally consists of, i.e. that glues together the container and
its elements? The answer is the "rebind<>" member template class a conforming
allocator has to have.

Cheers,
Philipp.

but to my knowledge if I use an allocator, only the items are in shared
memory not the container itself. In my case teh container also must lie
in shared memory to survive a process failure :-(

Is there any solution for containers in shared memory using
smart pointers? Where can I find templates?

If you can allocate your shared memory at a fixed address in each
process' address space, then you just need to write a custom allocator
to allocate from that memory. But if you can't allocate at a fixed
address, then you have to write a complex allocator that uses "offset"
smart pointers (and references!) that store an offset into the shared
memory rather than a pure pointer, and possibly a "shared memory ID"
or similar if you want to have more than one shared memory area in any
process. This is hard to do in a reliable way since C++ doesn't really
support smart references.



Jul 22 '05 #8
The T++ I've referenced seems to use a shared memory pool of a fixed size,
if I correctly understood its source code. But check this out, please:
<http://allocator.sourc eforge.net/>. This looks quite promising, doesn't
it?

Cheers,
Philipp.
but I still have the problem with pointers inside the container,
whereas pointers pose problems since the shared memory may have
different addresses in different processes :-(
that's true, but imposes no problem at all, because there's "placement new",
i.e. given we talk about Unix and given there's some magic "shmAllocator<> ",
you can write sth like

...
typedef std::map< int,std::less< int >,shmAllocato r< int > > shmIntMap;
shm=shmget(shm_ key,sizeof(shmI ntMap),IPC_CREA T|0700);
void *const p=(shmIntMap *)shmat(shm,NUL L,0);
shmIntMap *m=new(p) shmIntMap;
...

Then, don't forget to manually call the destructor afterwards.

Now, you might ask, o.k., now both the map and the ints contained within the map
go into the shared memory - but what's about the linked structure that establishes
the tree the map internally consists of, i.e. that glues together the container and
its elements? The answer is the "rebind<>" member template class a conforming
allocator has to have.
but to my knowledge if I use an allocator, only the items are in shared
memory not the container itself. In my case teh container also must lie
in shared memory to survive a process failure :-(
>Is there any solution for containers in shared memory using
>smart pointers? Where can I find templates?

If you can allocate your shared memory at a fixed address in each
process' address space, then you just need to write a custom allocator
to allocate from that memory. But if you can't allocate at a fixed
address, then you have to write a complex allocator that uses "offset"
smart pointers (and references!) that store an offset into the shared
memory rather than a pure pointer, and possibly a "shared memory ID"
or similar if you want to have more than one shared memory area in any
process. This is hard to do in a reliable way since C++ doesn't really
support smart references.

Jul 22 '05 #9
On Tue, 13 Jan 2004 15:05:50 +0100, Michael Schuler
<Mi************ *@fujitsu-siemens.com> wrote:
Thanks Philipp, good hint,

but I still have the problem with pointers inside the container,
whereas pointers pose problems since the shared memory may have
different addresses in different processes :-(


Solution 1 (easy if your OS supports it):

Map the shared memory to the same address in every process that
accesses it. Some OSes allow you to at least attempt this, and if it
fails you can bail out.
Solution 2 (a nightmare, not really worth it?):

Assuming your standard library is up to it (the only one I'm aware of
is Dinkumware, Metrowerks might be, libstdc++ and STLport aren't), it
might be possible to write a smart pointer and smart reference that
work using an offset into the shared memory (which will be the same
for every process). You'll obviously have to use those classes for any
other pointers/references you want to put in shared memory. I wrote
some such class templates a while back, with mixed success.
Solution 2 (probably best if you can't map to a fixed address):

Don't use the STL, but your own simpler code. Use explicit offsets,
not pointers.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4402
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py
3
2142
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++. It was ported from UNIX to run under Windows using .NET. If for no other reason than as an educational exercise, I have been wondering what it would take to rewrite the system under C#. The system currently has about a 100K “shared...
1
2113
by: myren, lord | last post by:
When I first discovered shared memory (between multiple processes) I immediately started thinking of how to build my own VM subsystem + locking mechanisms for a large single block of memory. This seems like one option, the other appears to be just having each "object" you want to share be a shared mem space to itself: allocate objects into a defined shared mem space. But here you have many many objects being shared. Having a VM...
14
8207
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At present the shared memory contains a few fixed-size structs, but I really need to be able to store more complex variable-sized data in there. So the next task is to work out how to store C++ objects, and if possible STL containers, in this shared...
12
5537
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine with 64 Gb of memory with a single non-partitioned database using extended storage and with intra-parallelism enabled. I've been experimenting with changing various parameters in an attempt
5
7077
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am getting this error when I try to run the command 'db2 "backup database dbname online to /opt/BACKUP"' on my 3 databases: SQL1042C An unexpected system error occurred. SQLSTATE=58004
4
3110
by: herbert | last post by:
I am coding a dozen "background" realtime apps for factory automation in .NET 2.0. The apps need to share a common memory as there are lots of variables to be shared (and synchronized of course). I need distinct apps for reasons like user interaction/access rights, error compartments, and because the size of the code, access to various ports, ... In the end the apps should run as Windows Service(s).
21
8374
by: llothar | last post by:
Hello, i need to manage a heap in shared memory. Does anybody know about a portable (win32+mac+posix) c implementation for this.
5
587
by: Sune | last post by:
Hi all, I want to make data stored in-memory (not disk) available to several processes. My concern is that poorly written C applications with dangling pointers may(will) damage the data in this memory segment if it is open to all, i.e. shared memory mapped into all processes memory area. I don't want to use TCP/IP client/server between the apps and a data store process due to the overhead.
0
8631
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8984
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.