-
Notifications
You must be signed in to change notification settings - Fork 761
multithreads in http #220
Comments
What is the signature of query_thread? |
shared_ptr<HttpServer:: Request> is Ok |
I could not see anything wrong after a quick look, but making a copy of the |
Ah, the problem seems to be that the Res shared_ptr is kept alive too long. The response is sent when the shared_ptr is destructed, and it seems like your Res object is kept alive in a scope higher than the scope of the handler. |
Yes,I want to store the Res object in order to use it after the handler. |
In that case, you need to use Response::send to send the data you have added to the response stream to the client. See https://github.com/eidheim/Simple-Web-Server/blob/master/http_examples.cpp#L183 for an example. |
This is the server_http.hpp that I used. |
Sadly that would be too inefficient. The data is sent when Response::send is called, or when the Response object within the shared_ptr is destroyed. |
When I am in query_handler() scope,obviously the response and request object are valid wherever they are referred(for example, Task , tasklines, the daemon thread). However, when I leave query_handler() scope, the response and request object are invalid wherever they are referred. |
I have read the https://github.com/eidheim/Simple-Web-Server/blob/master/http_examples.cpp#L183 example. |
The purpose of shared_ptr's is typically to extend the lifetime of an object. When you pass the shared_ptr to query_thread, the shared_ptr is copied and thus the object's lifetime is extended. You should in general not need to store shared_ptr's globally. |
However, I need thread scheduling. So I only store them in query_handler(). |
For example,if there are 60 queries but I only have 30 threads in the threadpool. If I directly pass the shared_ptr to query_thread().Then there are 30 shared_ptr that can't pass.I must store them. |
Regarding a thread pool of size 30, here is a simplified example of how I would implement this using asio: #include "server_http.hpp"
class Workers {
public:
boost::asio::io_service service;
private:
boost::asio::io_service::work work;
std::vector<std::thread> threads;
public:
Workers(size_t number_of_threads) : work(service) {
for(size_t c = 0; c < number_of_threads; ++c) {
threads.emplace_back([this] {
service.run();
});
}
}
};
using namespace std;
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
int main() {
Workers workers(30);
HttpServer server;
server.config.port = 8080;
server.default_resource["GET"] = [&workers](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
workers.service.post([response] {
this_thread::sleep_for(chrono::seconds(5)); // Simulate time-consuming work
response->write("Work done");
});
};
server.start();
} The response-shared_ptr is here copied by capture in |
In this way,if 30 threads are running,then what about the new shared_ptr ? |
If use workers.service.post, how should I sort these handlers in io_service. |
So, when I leave this scope, the response shared_ptr becomes invalid. |
I find a problem when I use server_http.hpp(old version) in http.
"response" is a shared_ptr<HttpServer:: Response> type. In this way, *response can send data to the client.
However,
When I let "Res" equal to "response",*response can not send data to the client.
Why I can't pass a shared_ptr<HttpServer:: Response> to another.
The text was updated successfully, but these errors were encountered: