C++ IPC question

I put this under game programming too because game programmers might know how to do this.

I need to write a class that creates an object, lets say the type of the object is MACHINE.

This class needs to take this instance of the object and pass it to another process somehow so both processes can access the same instance of MACHINE.

How do I do this?

I know some methods of IPC like using sockets, shared memory, message passing, but I don't think this will help me share an object.

Thanks for any help experts! I will be keeping an eye on this thread.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If multiple platforms, you can use CORBA
Search on "shared objects" in this link:

On a single platform in a multithreaded application, you can share the object directly amongst the threads.

I have heard that boost can facilitate this goal.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jeffiepooAuthor Commented:
It looks like CORBA is for java. I'm writing this in C++, it also seems kind of involved.

These are separate processes, not separate threads so I can't just have them sharing data.

I am having a single program launch a completely separate program using popen();


CORBA isn't really language or even OS specific:


Anyways, if you told us what operating system you are writing for and if the processes are running on the same computer, we might be able to point to some other alternatives too.

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

>> It looks like CORBA is for java. I'm writing this in C++
No, where supported, it is platform and language independent. (But for each language, you need the proper libraries.) Here's another link for C++:

>> CORBA isn't really language or even OS specific:
True, and that is the beauty of CORBA - it allows different platforms with developers writing in different languages to use a common interface to communicate. Java has their own (free) competing ORB (or at least, they used to).

>> it also seems kind of involved.
Yes, to be able to handle the multi-platform/multi-language communication, there is some learning curve.
That sounds a bit like a thread-safe COM object.  You don't actually need to involve COM directly, just use a COM style interface and make the functions thread safe (maybe use a mutex).  I guess the object itself would need to be located in shared memory.  I have no idea if that's relevant in this context.
If only dealing with a single platform, then CORBA is overkill.
Is the created object a POD-struct? If so, you can place this in shared memory and have the processes get a handle into the shared memory.
If you work on Windows, just use that Named Shared Memory, and implement simple synchronization using Mutexes...


Maybe explain why are you thinking that: "shared memory [...] doesn't help you share an object. This is exactly that method which you should use to share information between two local processes.
If you really need a generalized object sharing mechanism, then you will need to store the shared objects in a separate monitor/object-creation process that provides the api for the object's state and functionality. How you wish to synchronize requests is application dependent. (CORBA provides this for you, but since it is ultra-generalized and requires a significant learning curve, you are likely better off to customize your own middle-ware process yourself.)
I looked a little more into boost (some of which will be incorporated into the upcoming C++ release). I found this link which you may be able to use (sorry, as I said, I haven't used boost, yet):
    Creating named shared memory objects

"You want to create objects in a shared memory segment, giving a string name to them so that any other process can find, use and delete them from the segment when the objects are not needed anymore."

Some comments from the code example look promising:
      //Create an object of MyType initialized to {0.0, 0}

      //Create an array of 3 elements of MyType initializing each one
      //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
>> I know some methods of IPC like using sockets, shared memory, message passing, but I don't think this will help me share an object.

The question I would ask is : do you actually need to share the object ? If you don't have to, there's no need for making things complicated.

Can't you just pass messages between the processes to signal changes etc. ?
to add to above comments:

a shared object between two processes normally would require a server which manages the object. the clients are the two gamers which wouldn't communicate directly but always asking the server (process). that is called client-server and you exactly would need those things like sockets, message passing you mentioned. the advantage of a client server solution is that the clients may run on different computers and the server on a third computer. you also easily could add more clients or let the server manage more than one game a time.

the disadvantage infinity08 has told. it is more effort to program than a communication between two players only. you wouldn't have a shared object in this case but each player had its own copy of the object and would update it when it receives messages from the other player.

a third approach ranides has mentioned. if the players are on the same computer you could use shared memory to access the same data structure from both players. unlike to the client server solution it requires synchronisation techniques from both players, so it might be a little more difficult than message processing.


jeffiepooAuthor Commented:
AriMC: Running on Linux, and they ARE running on the same computer.

phoriffic: No it is not a POD struct, it is a class that handles a struct (linked-list) with all of the correct management methods that are needed for a linked list, this also goes to everybody:

Everybody: I can't use what ranides mentioned because I would need to share the object not the struct, because I need all of the link list's managment methods.

CORBA is an excellent suggestion, and I will look into that in the future if I have any real-world programming challenges. Thank you phoffric.

I looked into the boost library example as well, and that is the only thing that matches exactly what I was looking for in this post.  It pops an entire object, data structures, functions and all into a shared memory segment which is what I was looking for.

In the meantime I implemented it a different way, I just used shared memory segments that pass strings back and forth. The process has to decrypt the message and use it's own methods on it in a specific way which was a little more work. I started this post because I didn't want to have to write another process to manage the linked list...

So I guess I did it Infinity08's way, but the thing is, it made it MORE complicated than sharing the object would have, even though there would have been a little of a learning curve.

Anyway thanks everyone, especially phoffric for the great post. I'll look more into boost in the future.

>> So I guess I did it Infinity08's way, but the thing is, it made it MORE complicated than sharing the object would have

That depends on how you do it :) If you implemented this from the ground up, then it will of course have taken more work than a ready-to-use solution.

It also depends on how much communication there has to happen. There's a point where sharing the entire object becomes more convenient indeed, but then you can also ask the question if you really need that much communication. There's usually a way of simplifying things to the basics.
if you don't want a server process both clients have to manage their data separately anyway but you could share the data of that object via shared memory.

note, the boost sample doesn't have synchronisation mechanisms (if i didn't oversee them). so you would need to add exclusive access to the shared data yourself. if you have your own linked list it is not difficult to create the nodes and data in shared memory and make each access exclusively by using a named mutex.

jeffiepooAuthor Commented:
Yes, synchronisation mechanisms would have to be implemented as well, and have been.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.