#include <cstdlib>
#include <iostream>
#include <unistd.h>

#include "ThreadPool.h"

#define N 10
#define NTHREADS 2

using namespace std;
using hw3::ThreadPool;

void Printer(ThreadPool::Task *t);

class PrintTask : public ThreadPool::Task {
public:
  PrintTask(int i) : Task(Printer), i(i) {}
  int i;
};

int main(int argc, char *argv[]) {
  ThreadPool pool(NTHREADS);
  
  for (int i = 0; i < N; ++i)
    pool.Dispatch(new PrintTask(i));
  
  // XXX What's wrong with this?
  // XXX Why does the printing seem to be done serially?

  // sleep(1); // XXX What does this do for the execution?

  return EXIT_SUCCESS;
}

void Printer(ThreadPool::Task *t) {
  PrintTask *pt = reinterpret_cast<PrintTask*>(t);
  cout << pt->i << endl;
  sleep(1);
  delete pt;
}