我担心由于对象的使用方式,此代码没有执行我想要的操作
我有以下代码,我想知道是否有人可以帮我看一下。
我有一个多线程应用程序,它们都共享一个对象并对其进行操作。 我创建了一个指向它的某个元素的指针,这样我就不必每次都输入长路径,但我担心它可能只是修改共享对象的副本,而不是共享对象对象本身。
这是代码:
RPCThread* thr = &(args->s->_shared-_>rpcThread[args->threadIndex]);
...
thr->_in_use = true;
...
sema_post(&(thr->_sem_result));
这是有效的还是只是修改副本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于成员 _rpcThread 的类型。 如果只是 RPCThread[] 或 *RPCThread
那我不认为你有问题。 如果它是类类型那么你需要
了解其operator[]的返回类型。 如果相关定义是
返回一个值而不是引用,您可能有一个副本。
当然,除非 RPCThread 是一个使用信封字母惯用法或实现虚拟代理的类。
如果 _rpcThread 只是一个数组,那么您不应该在这里遇到您所询问的那种别名问题。
这是一件好事,无需阅读更多代码即可进行检查。 您能否将 this: 更改
为 this:
而不会导致编译时错误?
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:
to this:
without causing a compile-time error?