开发者

Threads in OpenMP C++

开发者 https://www.devze.com 2023-02-02 21:48 出处:网络
I need to achieve the C# threads effect in C++ OpenMP.. Thread t=new Thread( func1 ); t.Start(); // Do something

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..开发者_Go百科

Can I do this in C++ OpenMP ??

Thanks,


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
//...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号