OpenMP C++ 中的线程
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenMP 是比 C# 线程更高级别的线程库,通常用于几乎自动向串行应用程序添加一些线程。通过使用 #pragma omp parallel 指令,您也许能够实现与您想要的类似的效果,该指令将在多个线程中自动运行指令块内的代码。然后您可以在此并行部分中调用该函数。
OpenMP 的优势在于可以通过几个指令轻松地将线程添加到现有代码中。然而,我发现如果我想做任何复杂的事情(或者简单的事情),使用较低级别的线程库会更容易。
如果您想要与 C# 线程具有类似接口的东西,请查看 Boost.Thread 库。有了这个,你可以用几乎相同的语法做你想做的事:
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: