使用 C++ 进行多线程处理应用程序编程接口
我正在尝试使用 OpenMP 并行我的程序,有时我觉得我已经走进了死胡同。
我想共享我在类中定义(并初始化)的函数成员中的变量。如果我理解正确,则不可能对数据成员(例如 int
、boost::multi_array
和一个类的 std::vector
)。 例如:在类中的向量数据成员上使用push_back()。 更新 boost::multi_array
的值。
我的问题是 OpenMP 是否是合适的工具,或者我应该使用 boost::thread 还是 tbb?或者其他什么...支持 C++ API
Reagrds
i am trying to parallel my program using OpenMP and sometimes i feels that i am reaching a dead end.
I would like to share variables in a function member that i defined (and initialized) in the class. If i understood correctly, it is not possible doing #pragma omp parallel shared(foo)
of data members (e.g. int
, boost::multi_array
and std::vector
) of a class.
e.g.: using push_back() on a vector data member in the class.
updating values of a boost::multi_array
.
My question is if OpenMP is the right tools for it, or should i use boost::thread or tbb? or something else... what support C++ API
Reagrds
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如文档所述,
共享
定义对象是仅在内存中放置一次。例如,如果您的foo
对象包含某种类型的std::vector
,则应该完全可以push_back
循环内的项目。但您应该确保您的代码是线程安全的,无论是通过原子指令还是通过互斥体部分。As the documentation states,
shared
defines that an object is placed only once in the memory. For example if yourfoo
object contains astd::vector
of some type, it should be perfectly ok topush_back
items within the loop. But you should make sure, that your code is thread safe, either by atomic instructions or with mutex sections.