简体   繁体   English

将指针返回数据时,C ++的关键部分无用

[英]C++ Are Critical Sections Useless When returning Pointers to data

I think I already know the answer to this, but I'm currently working on re-vamping some legacy Visual C++ 6.0 code, and I was working on putting in some CCriticalSections, But then I got thinking about it, if all my function does is return a pointer to some data, I think the critical sections is probably useless, because I don't think im really protecting the data its pointing too, I don't think it really stops anyone from writing to it. 我想我已经知道了答案,但是我目前正在努力重新构建一些旧的Visual C ++ 6.0代码,并且正在尝试放入一些CCriticalSections,但是如果我的所有功能都可以,我就开始考虑它了。是返回指向某些数据的指针,我认为关键部分可能没有用,因为我认为即时消息并没有真正保护数据的指向,我认为它并没有真正阻止任何人对其进行写入。 (assuming i have the same locks around the setter). (假设我在二传手周围有相同的锁)。

What do you think? 你怎么看?

Yes if you are returning pointers to a data then deferencing the pointers outside the critical section would be a problem. 是的,如果您要返回指向数据的指针,则将指针推迟到关键部分之外将是一个问题。 Access to the critical shared data must be within the locks. 对关键共享数据的访问必须在锁之内。

I'm not sure what you mean by CCritialSections, but in general, functions that return pointers or references are not good candidates for mutex locking. 我不确定CCritialSections是什么意思,但通常来说,返回指针或引用的函数不是互斥锁的理想选择。 You need to manage things at a higher level. 您需要在更高层次上进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM