W3C home > Mailing lists > Public > www-lib@w3.org > July to September 2003

Re: multi-threads using libwww

From: Richard Atterer <richard@list03.atterer.net>
Date: Tue, 9 Sep 2003 10:56:30 +0200
To: www-lib@w3.org
Message-ID: <20030909085630.GD3261@nenya.lan>

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 23 April 2007 18:18:43 GMT