#include <iostream>
#include <pthread.h>
#include <chrono>
#define NUM_THREADS 5
void *threadFunction(void *threadID) {
long tid;
tid = (long)threadID;
std::cout << "Thread " << tid << " created." << std::endl;
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long i;
std::chrono::time_point<std::chrono::system_clock> start, end;
std::chrono::duration<double> elapsed;
start = std::chrono::system_clock::now();
for (i = 0; i < NUM_THREADS; i++) {
rc = pthread_create(&threads[i], NULL, threadFunction, (void *)i);
if (rc) {
std::cout << "Error: unable to create thread, " << rc << std::endl;
exit(-1);
}
}
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
end = std::chrono::system_clock::now();
elapsed = end - start;
std::cout << "Average time to create and terminate a thread: " << elapsed.count() / NUM_THREADS << " seconds." << std::endl;
pthread_exit(NULL);
}