0

Currently, I am trying to explore more about libevent server library.

I came across this example, which I ran with main as:

Program to run after downloading libevent library

main.cpp

#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;
}

5 threads get created in this by passing in CHttpServer server("127.0.0.1", 5555, 5);. I added a Sleep in void CHttpRequest::AddBufferOut(const char* data, size_t len) of 5 seconds, as shown below:

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" 

console text

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

The first five requests run parallel, but after that all requests get assigned to same thread. This case occurs when the sleep is more than 1 second, and doesn't when it is less one second. Any help would be appreciated. I am using windows 7 VS 2015. I am passing 10 requests using JMeter all request reach at once. It only prints "Hello world" on web http://127.0.0.1:5555/hello so time taken is not much. You can share your experience with a similar bug even if it was encountered in different Programs and measures you had taken.

Community
  • 1
  • 1
Kay
  • 13
  • 3
  • 1
    You are not showing the code that does the printing and you are not telling us what the output means. Given the current Information and what is missing (how many clients connect, how long are they connected, how much data do they send/receive) it is hard to tell what is going on..If you are limiting to 5 threads then I am not surprised that you only get 5 threads. And if there are 4 long time connections then the short time connections might be handled in the (only free) same (fifth) thread. – Werner Henze Jun 04 '19 at 12:45
  • Thank you @WernerHenze for your time. but it would be great if you can download both this Library and try coz i can't add every file of library. As you can see main.cpp is quite Simple which prints hello world on web.that's it.so time taken is not much and i have mentioned about JMeter in my question. Only one thread gets request while other are idle. If they would had taken more time then same issue would had occurred during sleep of less than 1 second – Kay Jun 04 '19 at 13:01
  • Your second link is to a github profile, instead of a project. Did you mean to link to https://github.com/frknyldz/http-server-library ? – Not a real meerkat Jun 04 '19 at 13:15
  • Yes I guess some has changed it. I have edited it back – Kay Jun 04 '19 at 13:29
  • Using iostream in signals, really? – curiousguy Jun 04 '19 at 14:09

0 Answers0