使用 C++ 进行多线程处理应用程序编程接口

发布于 2024-11-17 11:07:46 字数 351 浏览 10 评论 0原文

我正在尝试使用 OpenMP 并行我的程序,有时我觉得我已经走进了死胡同。

我想共享我在类中定义(并初始化)的函数成员中的变量。如果我理解正确,则不可能对数据成员(例如 intboost::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2024-11-24 11:07:46

正如文档所述,共享定义对象是仅在内存中放置一次。例如,如果您的 foo 对象包含某种类型的 std::vector,则应该完全可以push_back 循环内的项目。但您应该确保您的代码是线程安全的,无论是通过原子指令还是通过互斥体部分。

As the documentation states, shared defines that an object is placed only once in the memory. For example if your foo object contains a std::vector of some type, it should be perfectly ok to push_back items within the loop. But you should make sure, that your code is thread safe, either by atomic instructions or with mutex sections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文