On Thursday, October 27, 2011 09:29:52 PM Sven Geggus wrote: > Hello, > > I try to use libmicrohttpd in conjunction with a main function which > acquires data in an endless loop (currently every 4 seconds). > > What I want to achieve is that all the current client connections > with an active request for this data will block until it is > available. > > Thus I decided to go for the MHD_USE_THREAD_PER_CONNECTION model.
Well, that's technically not required. You can simply integrate your data- polling loop into the external select loop of MHD and in the response callbacks for data-aquisition return 0 (which will be interpreted as "no data available yet, try again in the next round of select"). Note that MHD will then NOT put the respective sockets into your write set, so you would not end up busy-waiting. > I tried to use pthread_cond_wait and pthread_cond_broadcast to > implement the blocking scheme, but unfortunately this does not work > as expected when more than one client is in use. I think to begin with, you need a mutex to protect your condition variable (https://gnunet.org/svn/GNUnet/src/util/threads/semaphore.c has sample code) But this won't be all -- you'd need to count the number of active clients, and to make sure to signal them all once, you'll need a second semaphore to implement a barrier. Threads are complicated (and this is not an MHD-specific problem). It might be easier to use the external-select mode for this problem. Happy hacking, Christian
