Edit: You are capturing vector by reference (to object) that is going to be destroyed after your loop iteration ends, if you are lucky and function call inside lambda has already copied it - it works, if you are unlucky - there's nothing to copy and you get a crash. Given that you do want to use a copy anyway I suggest capturing it by copy into lambda and then passing it by ref:
void TestThread(const std::vector<float>& data)
{
std::cout << data.size();
}
void Thready ()
{
vector<thread> threads(10); // this actually varies at runtime.
for (int i = 0; i < 10; i++)
{
float* data = HeapsOfFloats();
int numberOfFloats = NumberOfFloats();
std::vector<float> dataVector(data, data + numberOfFloats);
threads[i] = std::thread::thread([dataVector]{TestThread(dataVector)});
}
for (int t = 0; t < 10; t++) threads[t].join();
}
Bonus: your code (and mine) incurs two copies of float data but you could get away with at least one less copy if you can write in c++14:
...
threads[i] = std::thread::thread([dataVector(std::move(dataVector))]{TestThread(dataVector)});
...
For pre C++14 solutions look here or wrap your vector in smth like shared_ptr that would be cheaper to copy
I don't think there's anything wrong with the way you grab your floats (if only you spawn way too many threads).
However I think your problem is elsewhere: you are destructing threads at each iteration, but they have to be manually detached or joined to avoid termination, sometimes you are lucky and they run to termination before your loop cycle ends, sometimes - you are not. Just consider detach at the end of iteration if you don't care about them and dont plan to use thread handles later