当前,我正尝试探索有关libevent server library的更多信息。
我遇到了这个example Program to run after downloading libevent library,与main一起运行的是:
#include <winsock2.h>
#include <windows.h>
#include<stdio.h>
#include <csignal>
#include <condition_variable>
#include "HttpServer.h"
#pragma comment(lib,"WS2_32")
std::mutex m;
std::condition_variable cv;
void signalHandler(int signum)
{
cv.notify_all();
}
void onHello(CHttpRequest *req, void *arg)
{
char *data = "<html><body><center><h1>Hello World </h1></center></body></html>";
req->AddBufferOut(data, strlen(data));
req->SendReply(HTTP_OK);
}
int main(int argc, char** argv)
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
CHttpServer server("127.0.0.1", 5555, 5); //Create five threads
server.SetCallback("/hello", onHello, NULL);
server.Start();
std::unique_lock<std::mutex> lk(m);
cv.wait(lk);
return 0;
}
通过传入CHttpServer server("127.0.0.1", 5555, 5);
在其中创建 5个线程。我在void CHttpRequest::AddBufferOut(const char* data, size_t len)
中添加了5秒的睡眠,如下所示:
evbuffer_add(evhttp_request_get_output_buffer(this->req), data, len);
std::this_thread::sleep_for(std::chrono::milliseconds(5000)); // In place of "evthread_use_pthreads" replaced with "evthread_use_windows_threads"
Thread-id created in Lambda Expression -----> 1696
Thread-id created in Lambda Expression -----> 3008
Thread-id created in Lambda Expression -----> 4464
Thread-id created in Lambda Expression -----> 5516
Thread-id created in Lambda Expression -----> 1652
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 5516
Sending Reply by Thread-id------> 1696
Sending Reply by Thread-id------> 4464
Sending Reply by Thread-id------> 1652
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
前五个请求并行运行,但是之后所有请求都分配给同一线程。这种情况在睡眠时间超过1秒时发生,而在睡眠时间少于1秒时则没有发生。任何帮助,将不胜感激。我正在使用Windows 7 VS 2015。 我正在使用JMeter传递10个请求,所有请求一次到达。它仅在网络“ http://127.0.0.1:5555/hello”上打印“ Hello world”,因此花费的时间并不多。即使您在不同的程序和措施中遇到过类似的错误,也可以与您分享经验。