libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Sleeping a request thread


From: Kenneth Mastro
Subject: Re: [libmicrohttpd] Sleeping a request thread
Date: Wed, 24 May 2017 07:57:28 -0400

I strongly suspect the problem is in your code.

I created a fairly complex 'long polling' web server using the 'thread per connection' mode of MHD and it does not have this problem.  I.e., many times the client makes a request and it takes 30 seconds for it to get a response.  Other threads have no issues, and would not have any inherent issues with other threads 'sleeping' using any threading on any system that I'm aware of.

Unless something has changed, during initialization with 'thread per connection' mode, MHD creates all of those threads up front.  They just sit idle until they need to service a connection.  (At least I think that's what it does - I dug into it at one point a couple years ago.)  As a result, it seems pretty unlikely you're suddenly hitting some kind of system-level thread limit when you create other threads in your application since those MHD threads don't go away once the connection is closed - they just wait for another connection.


What you're describing sounds like one of 3 things:

1) You don't have enough threads allocated for your 'thread per connection' server and clients are stalling because MHD blocks them until a thread is freed up to service the connection.  (For example, browsers often open MULTIPLE connections to your server to 'pipeline' multiple requests and speed things up for the user.  That causes multiple threads to be consumed.)  Try increasing the number of threads in your MHD initialization and see if that fixes your problem.  However, as you describe it, this doesn't seem too likely.

2) Exceedingly unlikely when using 'sleep', but it's possible the thread scheduler in your OS is not yielding to other threads until one of the MHD threads finishes what it's doing.  I sincerely doubt this is the case, though.

3) You have some kind of cross-threading locking mechanism that is preventing one thread from continuing while another is ongoing and/or is waiting for it to complete.  Solving this is entirely application dependent - you'll just have to study how your threads interact.


To be clear - I really don't think MHD itself could cause the problem as you're describing it.  MHD threads are really just threads.  Generally speaking, threads don't cause other threads to stall/wait-on-each-other unless your code makes them do that.  The problem is very likely elsewhere.


Ken


On Wed, May 24, 2017 at 4:53 AM, Miguel Sancho <address@hidden> wrote:
Christian, thanks a lot for your answer,
here are more details, maybe you have seen something similar before?
I am seeing some issue with my process when I sleep the MHD request thread in THREAD_PER_CONNECTION mode
  • my process includes other threads reading from sockets which have nothing to do with MHD
  • seems that when I "sleep" the MHD request thread, this has an impact in other threads of the process, the messages get somehow "stuck" until the sleep in the MHD request thread finishes
  • the effect is the same, no matter if I execute a sleep() or a "long" while() (lasting for example 4s)

May the MHD thread sleep have an impact in other threads not related to MHD, any approach to work-around this?

Miguel


2017-05-19 0:22 GMT+02:00 Miguel Sancho <address@hidden>:
Hi,
using the MHD_USE_THREAD_PER_CONNECTION mode,
are there any issues in "sleeping" the request thread some seconds waiting for response? any known drawback in MHD?

for example to wait 4s:
         usleep(4000000)
         MHD_queue_response (..)

Thanks



reply via email to

[Prev in Thread] Current Thread [Next in Thread]