pass c++ objects to thread via DO

Apprentice
Posts: 8
Joined: 2007.02
Post: #1
Hi all,

I am trying to use Distributed Objects to communicate between threads. I think I would just like to somehow send copies of the c++ objects to my child thread to be
processed so that I can make changes to the objects currently in my main thread.

If I send the pointers, then while the thread is processing those objects, it could be that some of the member values of my c++ objects could change and throw things out of sync for my child thread which is working on the same data. Therefore, I've chosen to somehow pass by copy instead of pass by reference.

So I thought I would subclass NSMutableArray and try to create a custom class that implements NSCoding to encode and decode my c++ objects. But I've been reading the docs and I'm not completely clear on how this works and have the following questions.

1) When is my array of objects actually encoded? Does the encodeWithCoder: method get called automatically when I try to send my array over my DO connection? And the encoding happens all at once?

2) When I do call the DO method in my thread to send my custom array class, does initWithCoder: get called automatically upon arriving in the thread class?

3) I'm running into a lot of confusion on the c++ classes. I understand that I can't send them over DO (unless they're somehow wrapped), but I don't understand the encoding and decoding to get them across the connection to the other thread. I thought my custom subclass of NSMutableArray would contain the following methods.

Code:
- (void)encodeWithCoder: (NSCoder *)encoder
{
        // enumerate through NSMutableArray and encodeObject: on each object.

}

- (id)initWithCoder: (NSCoder *)decoder
{
        // enumerate  through NSMutableArray and call decodeObject.
        // return NSMutableArray of decoded objects.
}

What I don't understand is the process of encoding my c++ object after adding it to the array as such ...
Code:
[gtPatterns addObject:[NSNumber numberWithLong: (long)pattern]];

This is just the pointer as a long? If so, I don't have a copy of the object here. What do I encode in the encodeWithCoder: method from this?

I guess once I understand the last part, I can rebuild/decode from this on the receiving thread the c++ classes and go from there.

And if anyone happens to know of any encoding, decoding of array
examples, that would be much appreciated.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
The pointer can be represented as an unsigned long. (or a long, which is typecasted to an unsigned long) If you have them as objects rather than pointers to objects, you will have to take their address first.

If you're squeamish about using the same copy of the object, you can either use locks to make sure that the object doesn't modify itself more than once at a time, or if you don't want to modify the original at all, you can make a copy constructor and have each thread have its own copy. (either on the stack or on heap)
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
Do not turn a pointer into an unsigned long!

use NSValue valueWithPointer if you have to.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
Why not? An unsigned long is the same size as a pointer. (well, I think 64 bit Windows is the only exception, since it's still 32 bits)
Quote this message in a reply
Post Reply