简体   繁体   English

使用boost :: thread将c ++对象从一个线程传递到另一个线程的正确方法是什么?

[英]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? 将C ++对象(地图,矢量等)从一个线程传递到另一个线程的正确方法是什么? 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. 如果OTOH 确实包含对仍可能在原始线程中使用的某些数据的引用 - 其访问应该是同步的。

And, of course, passing a complex object by-value should be avoided. 当然,应该避免传递复杂的对象副值。

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

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