我担心由于对象的使用方式,此代码没有执行我想要的操作

发布于 2024-07-17 17:11:07 字数 358 浏览 5 评论 0原文

我有以下代码,我想知道是否有人可以帮我看一下。

我有一个多线程应用程序,它们都共享一个对象并对其进行操作。 我创建了一个指向它的某个元素的指针,这样我就不必每次都输入长路径,但我担心它可能只是修改共享对象的副本,而不是共享对象对象本身。

这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-07-24 17:11:07

取决于成员 _rpcThread 的类型。 如果只是 RPCThread[] 或 *RPCThread
那我不认为你有问题。 如果它是类类型那么你需要
了解其operator[]的返回类型。 如果相关定义是
返回一个值而不是引用,您可能有一个副本。

当然,除非 RPCThread 是一个使用信封字母惯用法或实现虚拟代理的类。

如果 _rpcThread 只是一个数组,那么您不应该在这里遇到您所询问的那种别名问题。

这是一件好事,无需阅读更多代码即可进行检查。 您能否将 this: 更改

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

为 this:

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

而不会导致编译时错误?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文