I just want to write a simple program in C++, which creates two threads and each of them fills vector by squares of integers (0, 1, 4, 9, ...). Here is my code:
#include <iostream>
#include <vector>
#include <functional>
#include <thread>
#include <time.h>
#define MULTI 1
#define SIZE 10000000
void fill(std::vector<unsigned long long int> &v, size_t n)
{
for (size_t i = 0; i < n; ++i) {
v.push_back(i * i);
}
}
int main()
{
std::vector<unsigned long long int> v1, v2;
v1.reserve(SIZE);
v2.reserve(SIZE);
#if !MULTI
clock_t t = clock();
fill(v1, SIZE);
fill(v2, SIZE);
t = clock() - t;
#else
clock_t t = clock();
std::thread first(fill, std::ref(v1), SIZE);
fill(v2, SIZE);
first.join();
t = clock() - t;
#endif
std::cout << (float)t / CLOCKS_PER_SEC << std::endl;
return 0;
}
But when I run my program, I see, that there is no significant difference between the serial version and the parallel one (or sometimes parallel version shows even worse results). Any idea what happens?

push_backwould need a lock.man clock(): The clock() function returns an approximation of processor time used by the program.I invite you to ponder the meaning of "processor time".