Re: multi-threads using libwww

On Mon, Sep 08, 2003 at 10:11:35PM -0700, Jerry G. Chiuan wrote:
> However, in my application I have 2 threads, one of them would send
> request to server and server would hold the request, I mean the sever
> will not respond instantly until there are really something which needs
> to be returned back to the client.  Therefore, single thread MUST not be
> enough in my case. I definitely need 2nd thread which can do other things
> while 1st thread is blocked

No - I agree with Fred, a single thread is enough if you use the libwww
event loop.

Basically, the way this works is by using the Unix select() call (Windows 
has something equivalent). With select(), you give *several* sockets to the 
OS at once and ask it to return to you if *any* of them has new data. 
Furthermore, you can prevent your single thread from being blocked by 
creating the socket with the O_NONBLOCK flag. With O_NONBLOCK, the read() 
and write() functions never block, instead they return a special code the 
moment they would otherwise block.

So in pseudo code, the event loop will look like this:

  while (1) {
    int socketNr = select(all_of_our_sockets);
    // Do things with the socket, e.g.:
    switch (socketNr) {
      ... read() or write()
    }
  }
  
In your libwww application, you don't need to worry about how this works in
detail - all you do is start a request. The respective HTLoad() call
returns immediately, and when the response data comes from the server 
later, it is passed to your callback function.

Cheers,

  Richard

-- 
  __   _
  |_) /|  Richard Atterer     |  GnuPG key:
  | \/¯|  http://atterer.net  |  0x888354F7
  ¯ '` ¯

Received on Tuesday, 9 September 2003 05:19:32 UTC