Skip to content

Instantly share code, notes, and snippets.

@rafaelcn
Last active March 11, 2018 16:51
Show Gist options
  • Save rafaelcn/1791d2e8bdf684b51be2ffc84d6e3752 to your computer and use it in GitHub Desktop.
Save rafaelcn/1791d2e8bdf684b51be2ffc84d6e3752 to your computer and use it in GitHub Desktop.
Sleep sort
// 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