简体   繁体   中英

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. (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. You need to manage things at a higher level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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