OpenMP C++ 中的线程

发布于 2024-10-10 13:58:10 字数 210 浏览 2 评论 0原文

我需要在 C++ OpenMP 中实现 C# 线程效果。

Thread t=new Thread( func1 );
t.Start(); // Do something
// Do something else

请注意,父级或子级都没有等待加入。

我可以在 C++ OpenMP 中执行此操作吗?

谢谢,

I need to achieve the C# threads effect in C++ OpenMP..

Thread t=new Thread( func1 );
t.Start(); // Do something
// Do something else

Notice that neither the parent or the child waits to be joined..

Can I do this in C++ OpenMP ??

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

獨角戲 2024-10-17 13:58:10

OpenMP 是比 C# 线程更高级别的线程库,通常用于几乎自动向串行应用程序添加一些线程。通过使用 #pragma omp parallel 指令,您也许能够实现与您想要的类似的效果,该指令将在多个线程中自动运行指令块内的代码。然后您可以在此并行部分中调用该函数。

OpenMP 的优势在于可以通过几个指令轻松地将线程添加到现有代码中。然而,我发现如果我想做任何复杂的事情(或者简单的事情),使用较低级别的线程库会更容易。

如果您想要与 C# 线程具有类似接口的东西,请查看 Boost.Thread 库。有了这个,你可以用几乎相同的语法做你想做的事:

#include <boost/thread.hpp>

//creates the thread object and starts the thread, returns immediately
boost::thread th(func1);

//...
//do something else
//...

OpenMP is a higher level threading library than C# threads and is often used to almost automatically add some threading to serial applications. You may be able to achieve something similar to what you want by using the #pragma omp parallel directive which will automatically run the code within the directive block in multiple threads. You could then call the function in this parallel section.

The strength of OpenMP is that it is simple to add threading to existing code with a few directives. However, I have found it easier to use a lower level threading library if I want to do anything complicated (or easy for that matter).

If you want something with a similar interface to C# threads, take a look at the Boost.Thread library. With this, you can do what you want in almost the same syntax:

#include <boost/thread.hpp>

//creates the thread object and starts the thread, returns immediately
boost::thread th(func1);

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