简体   繁体   中英

What is the proper way to pass a c++ object from one thread to another using boost::thread?

what is the proper way of passing a C++ object (a map, vector, etc.) from one thread to another? I fear race conditions, stale values, etc. I am using boost threads.

Thanks in advance!

This depends solely of what this object "does". If it's self-contained, ie doesn't hold pointers/references to other objects that may be used in the originating thread - there seems to be no problem. If OTOH it does contain references to some data that still may be in use in the originating thread - its access should be synchronized.

And, of course, passing a complex object by-value should be avoided.

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