The char*
returned by c_str()
will be deleted at the end of the loop body. That means, the behaviour of the program is undefined, and only accidentally, you see the same value in each thread.
It's a lot easier to use threads with C++11. The code might look as follows:
std::vector<std::thread> threads;
for (int i = 0; i < numberOfFiles; ++i) {
std::stringstream filestream;
filestream << "file" << i + 1 << ".txt";
std::string message = filestream.str();
threads.emplace_back(&findLargestInFile, message);
}
void findLargestInFile(std::string rawFileName)
{
// ...
}