Skip to content

Instantly share code, notes, and snippets.

@nus
Created May 19, 2011 23:59
Show Gist options
  • Save nus/982060 to your computer and use it in GitHub Desktop.
Save nus/982060 to your computer and use it in GitHub Desktop.
Sleep sort
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
using namespace std;
using namespace boost;
/*
Sleep sort (http://dis.4chan.org/read/prog/1295544154)
$ ./slee_sort 8 2 4 5
2
4
5
8
only positive number
*/
void sleep_and_print(double ms)
{
this_thread::sleep(posix_time::milliseconds(ms));
cout << ms << endl;
}
int main(int argc, char *argv[])
{
thread_group t;
for(int i = 1; i < argc; i++) {
double ms;
try {
ms = lexical_cast<double>(argv[i]);
}
catch(...) {
continue;
}
t.create_thread(bind(sleep_and_print, ms));
}
t.join_all();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment