简体   繁体   中英

Threads in OpenMP C++

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM