Re: [Quickfix-developers] Usage of non-threadsafe C functions in QF
Brought to you by:
orenmnero
From: Joerg T. <Joe...@ma...> - 2003-04-03 10:50:23
|
OM...@th... wrote: > It appears under windows they are not thread safe. > [....] > This looks like an important fix so it should probably go out with 1.4.1, > initially using locking for windows. May I provide some sample code from our software: For gethostbyname_r(() there are difference between Linux and Solaris: 102 /* Get the IP address of the localhost. 103 * Note: This thread-safe version needs some extra data structures. 104 */ 105 { 106 struct hostent host; 107 struct hostent *host_ptr = NULL; 108 char gethostname_buffer[ 1024 ]; 109 int error; 110 111 #ifdef SYSTEM_linux 112 gethostbyname_r( "localhost", &host, gethostname_buffer, sizeof( gethostname_buffer ), &host_ptr, &error ); 113 #endif 114 #ifdef SYSTEM_sunos 115 host_ptr = gethostbyname_r( "localhost", &host, gethostname_buffer, sizeof( gethostname_buffer ), &error ); 116 #endif 117 118 if ( NULL == host_ptr ) { 119 logError( "openSocketQueue: Could not resolve 'localhost'" ); 120 return -1; 121 } 122 123 memcpy( &sin.sin_addr, host_ptr->h_addr_list[0], sizeof( sin.sin_addr ) ); 124 } localtime() and gmtime() are simple, since you only have to provide an extra buffer instead of using the returned value. Note that there man pages may be missing on Linux. But you could use http://docs.sun.com to look up the Solaris man pages, which are equivalent here: SYNOPSIS #include <time.h> [...deleted...] struct tm *localtime_r(const time_t *clock, struct tm *res); struct tm *gmtime_r(const time_t *clock, struct tm *res); DESCRIPTION [...] The localtime_r() and gmtime_r() functions have the same functionality as localtime() and gmtime() respectively, except that the caller must supply a buffer res to store the result. [...] ERRORS The ctime_r() and asctime_r() functions will fail if: ERANGE The length of the buffer supplied by the caller is not large enough to store the result. Cheers, Jörg -- Joerg Thoennes http://macd.com Tel.: +49 (0)241 44597-24 Macdonald Associates GmbH Fax : +49 (0)241 44597-10 Lothringer Str. 52, D-52070 Aachen |