I have the following method on a class
void Listener::Start()
{
Logger logger;
std::string logMessage("Starting '" + to_utf8string(GetName()) + "' Listener");
http_listener httpListener(GetUri());
std::string listenerName(to_utf8string(name));
logger.log(logMessage);
// listener recieves a GET request.
httpListener.support(methods::GET, [listenerName](http_request request)
{
Logger logger;
std::string logMessage("GET request recieved from " + listenerName);
logger.log(logMessage);
// dummy line just till routing is dealt with
request.reply(status_codes::OK, logMessage);
});
// open listener and route request
httpListener
.open()
.then([&httpListener,listenerName](){
Logger logger;
std::string logMessage(listenerName + "started");
logger.log(logMessage);
}).wait();
// JUST WAIT - we do not want the application to stop
while (true);
}
Now I do not know how many threads there are - it is basically just the number of records read from a database table.
for each (Listener l in ls.Select(m.GetId()))
{
l.Start();
}
Only the first thread is started and runs, Which is kind of logical in that the only thing stopping the thread from running is a permanent loop.
If however it is run this way;
std::vector<thread> listener_threads;
for each (Listener l in ls.Select(m.GetId()))
{
listener_threads.push_back(thread{ &Listener::Start, &l });
}
None of the threads seem to be running - none of the listeners reply to any request.
So the question is how can you run an indeterminate number of threads in a C++ application.