views:

102

answers:

1

I have the following code and I was wondering if someone could look at it for me.

I have a multi-threaded application that all share an object and operate on it. I've created a pointer to a certain element of it, just so I don't have to type in the long path every time, but I'm concerned it might simply be modifying a copy of the shared object, rather than the shared object itself.

Here is the code:

RPCThread* thr = &(args->s->_shared-_>rpcThread[args->threadIndex]);
...
thr->_in_use = true;
...
sema_post(&(thr->_sem_result));

Is this valid or would this just be modifying a copy?

+2  A: 

Depends on the type of member _rpcThread. If it's simply RPCThread[] or *RPCThread then I don't think you have a problem. If it's a class type then you need to know the return type of its operator[]. If the relevant definition is returning a value rather than a reference, you probably have a copy.

Unless, of course, RPCThread is a class that uses the envelope-letter idiom or implements a virtual Proxy.

If _rpcThread is just an array, you shouldn't have an aliasing issue here of the kind about which you're asking.

Here's a good thing to check without doing much more code reading. Can you change this:

 RPCThread* thr = &(args->s->_shared->_rpcThread[args->threadIndex]);

to this:

 RPCThread* thr = args->s->_shared->_rpcThread + args->threadIndex;

without causing a compile-time error?

Thomas Kammeyer
Yes I can do that.
samoz
that bodes well... it's always possible there are some weird conversion operator overloads, but I guess the other thing to check is that different threads think they have the same address (log the address from different threads as a hex value, check for identity)
Thomas Kammeyer