Since the API is fairly similar between hpx
and #include<thread>
is it possible to have the same code be able to run hpx
or #include<thread>
?
Since boost is a requirement of hpx
, my use case here is there are systems that aren't allowed boost and some that are, I want the same code to run on both, but use hpx
if possible.
Assuming I'm only using features that are in both hpx and thread, Is there an example of doing this? Would I even be able to via #ifdef
?
If the API of both libraries is exactly the same, you can use a type alias that gets compiled conditionally:
#if defined(USE_HPX_THREADS)
#include <hpx/thread.hpp>
namespace my_library
{
using my_thread = hpx::thread;
}
#elif defined(USE_STD_THREADS)
#include <thread>
namespace my_library
{
using my_thread = std::thread;
}
#endif
Alternatively, if the APIs differ, you can create your own homogeneous interface, with different implementations depending on a preprocessor definition:
class my_thread
{
private:
// "Conditional type alias" like the example above.
my_thread_handle _thread;
public:
void join();
};
#if defined(USE_HPX_THREADS)
void my_thread::join()
{
_thread.hpx_join();
}
#elif defined(USE_STD_THREADS)
void my_thread::join()
{
_thread.std_join();
}
#endif
It could be a good idea to separate the implementations in different files. Check out libraries like SFML for a real-world example (Unix and Win32 folders).