In C++, you might want to use OS-specific APIs depending on the platform your code is running on. To achieve this, you can use preprocessor directives to conditionally compile code. Below is an example demonstrating how to implement this in a C++ program using the Windows and Unix operating systems.
#include
#ifdef _WIN32
#include
#elif defined(__unix__)
#include
#endif
int main() {
#ifdef _WIN32
std::cout << "Running on Windows" << std::endl;
Sleep(1000); // Sleep for 1000 milliseconds
#elif defined(__unix__)
std::cout << "Running on Unix/Linux" << std::endl;
sleep(1); // Sleep for 1 second
#endif
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?