Last active
March 11, 2018 16:51
-
-
Save rafaelcn/1791d2e8bdf684b51be2ffc84d6e3752 to your computer and use it in GitHub Desktop.
Sleep sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not platform dependent | |
#include <iostream> | |
#include <chrono> | |
#include <array> | |
#include <thread> | |
void print(int n, std::chrono::seconds s) { | |
std::this_thread::sleep_for(s); | |
std::cout << n; | |
} | |
int main() { | |
std::array<int, 7> v = {3, 4, 9, 2, 5, 1, 8}; | |
std::array<std::thread, 7> t; | |
for (int i = 0; i < 7; i++) { | |
t[i] = std::thread(print, v[i], | |
static_cast<std::chrono::seconds>(v[i])); | |
} | |
for (int i = 0; i < 7; i++) { | |
t[i].join(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment