#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int niterations = 50; int nobjects = 30000; int nthreads = 1; int work = 0; int objSize = 1;
class Foo {
public:
Foo (void)
: x (14),
y (29)
{}
int x;
int y;
};
void worker ()
{
int i, j;
Foo ** a;
a = new Foo * [nobjects / nthreads];
for (j = 0; j < niterations; j++) {
for (i = 0; i < (nobjects / nthreads); i ++) {
a[i] = new Foo[objSize];
#if 1
for (volatile int d = 0; d < work; d++) {
volatile int f = 1;
f = f + f;
f = f * f;
f = f + f;
f = f * f;
}
#endif
assert (a[i]);
}
for (i = 0; i < (nobjects / nthreads); i ++) {
delete[] a[i];
#if 1
for (volatile int d = 0; d < work; d++) {
volatile int f = 1;
f = f + f;
f = f * f;
f = f + f;
f = f * f;
}
#endif
}
}
delete [] a;
}
int main (int argc, char * argv[])
{
thread ** threads;
if (argc >= 2) {
nthreads = atoi(argv[1]);
}
if (argc >= 3) {
niterations = atoi(argv[2]);
}
if (argc >= 4) {
nobjects = atoi(argv[3]);
}
if (argc >= 5) {
work = atoi(argv[4]);
}
if (argc >= 6) {
objSize = atoi(argv[5]);
}
printf ("Running threadtest for %d threads, %d iterations, %d objects, %d work and %d objSize...\n", nthreads, niterations, nobjects, work, objSize);
threads = new thread*[nthreads];
high_resolution_clock t;
auto start = t.now();
int i;
for (i = 0; i < nthreads; i++) {
threads[i] = new thread(worker);
}
for (i = 0; i < nthreads; i++) {
threads[i]->join();
}
auto stop = t.now();
auto elapsed = duration_cast<duration<double>>(stop - start);
cout << "Time elapsed = " << elapsed.count() << endl;
delete [] threads;
return 0;
}