WinInet thread safe?

1510 views
Skip to first unread message

Jan Plastenjak

unread,
Apr 10, 2004, 11:08:17 AM4/10/04
to

Are WinInet functions thread safe? Which WinInet functions are not
thread safe?


Stephen Sulzer

unread,
Apr 10, 2004, 7:17:56 PM4/10/04
to

If you Google the newsgroups on this subject, you will get differing
opinions as to how thread safe WinInet is. The answer really is "it
depends." Internally, WinInet does try to be thread safe, but you have to
be careful not to put too much stress on the API.

(You don't mention what type of operations you want to use WinInet for--FTP,
HTTP, etc.--so I'll assume HTTP.)

Keep in mind that WinInet was designed only for single-user desktop client
applications. Do not use WinInet in any kind of multi-user or server-based
application, or NT service. For those kind of applications, you should use
either the .NET Framework or the WinHTTP API instead.

Within a single-user application or component, it is OK to send HTTP
requests concurrently across multiple threads with WinInet. You do not need
to serialize access to the InternetOpen and InternetConnect handles when
creating new HTTP request handles. WinInet handles do not have any thread
affinity, so you can access the same handle from different threads. However,
once an HTTP request is in-progress, you should be careful with
multithreaded access to the HTTP request handle.

During the HTTP request, it is a good idea to serialize access to the
WinInet request handle. In particular, you should avoid calling
InternetCloseHandle from another thread to abort an HTTP request that is
in-progress. To abort an HTTP request, close the request handle during a
callback (meaning you'll need to set a callback function on the request
handle via InternetSetStatusCallback).

If your application is going to send a lot of concurrent HTTP requests
simultaneously across multiple threads, then you should consider turning off
the WinInet cache (by specifying the INTERNET_FLAG_RELOAD and
INTERNET_FLAG_NO_CACHE_WRITE flags in the call to HttpOpenRequest).

Also note that by default, WinInet will limit the number of simultaneous
connections to a given server (or proxy) to 2 simultaneous connections, per
the HTTP/1.1 specification. So if you try to send more than two requests to
the same server, WinInet will let two of the requests go through and block
the others. (This limit can be overridden using the
INTERNET_OPTION_MAX_CONNS_PER_SERVER and _MAX_CONNS_PER_1_0_SERVER options).

If you do experience instability using WinInet in multithreaded scenarios,
then switch to WinHTTP. WinHTTP also does not have the default
2-simultaneous connection limit.

Hope that helps.

Stephen


"Jan Plastenjak" <no...@ms.com> wrote in message
news:%23eUgo2w...@TK2MSFTNGP12.phx.gbl...

Jan Plastenjak

unread,
Apr 13, 2004, 5:46:52 AM4/13/04
to

Thank you very much.


Reply all
Reply to author
Forward
0 new messages