#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>

#include <iostream>

#define LOOPCOUNT 100000

void* NullChild(void*) { return nullptr; }

int main(int argc, char** argv) {
  int forkcount = 0;
  struct timeval before, after;

  gettimeofday(&before, nullptr);  // once to warm up call path
  gettimeofday(&before, nullptr);  // once for real-sies
  while (1) {
    pthread_t thr;

    pthread_create(&thr, nullptr, &NullChild, nullptr);
    pthread_join(thr, nullptr);
    forkcount++;
    if (forkcount == LOOPCOUNT)
      break;
  }
  gettimeofday(&after, nullptr);

  float diff_us = (after.tv_sec - before.tv_sec) * 1000000.0;
  diff_us += (after.tv_usec - before.tv_usec);
  diff_us /= LOOPCOUNT;

  std::cout << "microseconds per thread create: " << diff_us << std::endl;
  return EXIT_SUCCESS;
}