I am learning multithreading in C ++ and trying to set up a thread pool, but I get a complier error message that says: "Error:" mapperNodes not written "and the command" error: "not written." I read a little about using "this" to capture variables in lambda, but so far nothing works.
How can I use the command and mapperNoders variables in the lambda function of the thread pool in the code below?
void MapReduceServer::spawnMappers() throw() { vector<string> mapperNodes(nodes); random_shuffle(mapperNodes.begin(), mapperNodes.end()); string command = buildCommand(mapperNodes[0], executablePath, mapperExecutable, mapOutputPath); ThreadPool pool(numMappers);//numMappers = 8 for (size_t id = 0; id < numMappers; id++) { pool.schedule([id] { cout << oslock << "Thread (ID: " << id << ") has started." << endl << osunlock; spawnWorker(mapperNodes[0], command); /*compiler error here*/ cout << oslock << "Thread (ID: " << id << ") has finished." << endl << osunlock; }); }
source share