libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] How to get response back to requesting thread?


From: Bob Furber
Subject: Re: [libmicrohttpd] How to get response back to requesting thread?
Date: Thu, 19 Mar 2015 12:01:50 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

Thanks for your suggestions, Ken. And, yes, I am using MHD-based-webserver as part of an the application where the processing of MHD requests is done in the main() thread.

Never having used condition variables before, I am not sure what you have in mind. Is it using a mutex with a condition variable? Or a semaphore as a condition variable? If so, what I don't understand is how main() can signal selectively to corresponding mhd_thread that it has a response ready for it.

Do you mean something along the lines of:

mhd_thread:
receives http_request_txt
pthread_mutex_lock
add http_request_txt to message_queue
pthread_cond_wait(condition,mutex)
get_response_txt_from_main
prepare and queue mhd_response
pthread_mutex_unlock

main()/main-loop:
if (http_request_txt in message_queue)
response = process(http_request_txt)
pthread_cond_signal(condition)


On your blocking_queue suggestion, am I correct in assuming that other mhd_threads are blocked by mutexes from accessing the response awaiting in the blocking_queue?

In the meantime, I shall research these 2 avenues.

But, before I sign off, would you happen to know if a callback is an option for getting the response from main() back to the src_mdd_thread?

Thx

On 15-03-19 04:57 AM, Kenneth Mastro wrote:
If I'm understanding you correctly, you want your connection thread in
MHD to essentially submit some data/request to a 'main loop' thread, and
for it to then wait for the main loop to process it before
returning/sending the response back to the client?  If so, and assuming
MHD and your main loop are just threads within the same process...  The
way I do that kind of thing is to use condition variables and/or a
blocking queue.

Your MHD thread would submit the request to the main loop (very likely
into a queue that the main loop processes), then wait for a response.
You can wait for a response by "waiting" on a condition variable that
the main loop will "notify" when it has completed processing the data.
('Wait' and 'notify' are the proper terms - you'll see them if you
search around for this stuff.)

Or, if you have access to one and/or want to write one, you can do
something similar using a blocking queue.  First, have your MHD thread
submit the request to the main loop (again, into some kind of queue).
Then, have your MHD thread read/pull from a one-item request-specific
blocking queue that the main loop will write/submit the response to.
(The blocking queue will likely use a condition variable under the
covers, by the way.)  In other words - when the main loop writes to that
request-specific blocking queue that the MHD thread is waiting on, the
MHD thread will wake up and pull it from the blocking queue to finish
processing the request - necessary data in hand.  This is probably
slightly less efficient than the raw condition variable stuff, but it
makes the code simpler because it allows you to hide that condition
variable from the code that's using it.

In both cases and if you want to, you should be able to 'wait' for a
maximum amount of time.  E.g., if it takes longer than 3 seconds to get
a response, the MHD thread could wake up anyway and submit an error
message back to the client.

This all may be a lot easier in C++ (using the std lib) than it is in C
- I'm not sure.  I know raw pthreads have support for condition
variables, but I've never played with them myself.  Writing a blocking
queue is not particularly difficult if you don't have one - less than 50
lines of code (at least that's how it is in C++).

If your MHD-based web server is in fact a separate process from your
'main loop' (not just separate threads), well.. then you're doing real
IPC, which can bring a whole host of pain.  The simplest (and likely
least efficient) way is probably just to use sockets and send messages
back and forth.  Search around for IPC methodologies to see what your
options are - I'm no expert (I vaguely recall a Beej guide to IPC that I
found informative).  MHD is so lightweight, I'd suggest going the
thread-based route, myself - just make your MHD-based-webserver part of
the application rather than a standalone process and use some kind of
thread-handoff methodology (e.g., like one of the 2 I suggest above).


Hope that helps.


Ken



On Wed, Mar 18, 2015 at 9:17 PM, Bob Furber <address@hidden
<mailto:address@hidden>> wrote:

    |I realize this is not really a MHD issue, but, it is likely an
    issue your readers have encountered.||

    I have a ||custom ||server [C] ||program ||on a Linux-Arm platform
    ||which uses MHD ||as its||front end to receive and respond to http
    requests.| |||

    Currently, each request is processed in its own thread ||to obtain
    the response||, with the processing code sandwiched inside a mutex
    to protect data from being altered by code in the main-loop ..which
    also accesses the same data. ||Think of the data as a home-baked
    database that is being altered by the main-loop AND by http
    requests.| |||

    Dreaming in Technicolor, I would like to have all the data
    processing done in the main-loop. That is, request||s||would be
    transferred from the connection threads to the main-loop by means of
    some IPC ||FIFO ||process such as a message queue or named pipe.
    ||My problem is||, I cannot figure out how the main-loop would get
    the response back to the requesting thread.||

    Again, d||reaming in Technicolor, ||I would like to use some magic
    IPC process where the MHD thread would tag the request with a
    return_response_to_this_pid ||return address ||to allow the main
    loop to deliver the response messages addressed to the corresponding
    corresponding MHD_thread ...an IPC method along the lines of
    send_ipc_msg( message, receiving_pid ). | |||

    Clues anybody?| I am not even sure how to ask the right question. ||






reply via email to

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