【发布时间】:2016-02-09 20:16:02
【问题描述】:
我遇到了从 Poco::HTTPServer 启动线程的奇怪行为
这是我得到的:
class Worker : public Poco::Runnable {
public:
Worker(std::string request): _request(request) {};
void run();
std::string GetResult();
private:
std::string _request;
};
void Worker::run() {
// do something with _request;
}
class RequestHandler : public HTTPRequestHandler {
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp) {
std::string request(static_cast<stringstream const&>(stringstream() << req.stream().rdbuf()).str());
Poco::Thread thread;
Worker worker(request);
std::string response = "";
thread.start(worker);
// parsing content of the request to detect if request is async or sync
if(async) {
// make dummy response
response = "your request queued";
} else { // if sync
thread.join();
response = worker.GetResult();
}
ostream& out = resp.send();
out << response;
out.flush();
}
private:
};
现在,如果请求是同步类型的,一切正常,RequestHandler 等待线程的加入,然后以工作者的结果响应,一切正常。但是如果请求是异步类型的,我不需要等待线程加入,我只需要在工作线程继续处理请求时发送一个虚拟响应。几个异步请求后的问题是段错误。像这样:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff2e4c700 (LWP 13986)]
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff7a3f76e in Poco::ThreadImpl::runnableEntry (pThread=0x7ffff464e930) at /home/uzurik/libs/poco-PostgreSQL/Foundation/src/Thread_POSIX.cpp:436
#2 0x00007ffff66c26aa in start_thread (arg=0x7ffff2e4c700) at pthread_create.c:333
#3 0x00007ffff69dfeed in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
我做错了什么?
【问题讨论】:
标签: c++ multithreading thread-safety poco-libraries