Date: Wed, 28 May 2003 20:46:26 -0400 (EDT) From: Daniel Eischen <eischen@pcnet.com> To: Craig Rodrigues <rodrigc@attbi.com> Cc: freebsd-threads@freebsd.org Subject: Re: Question about OpenSSL id_function() and pthreads Message-ID: <Pine.GSO.4.10.10305282038470.5688-100000@pcnet5.pcnet.com> In-Reply-To: <20030529003416.GA3712@attbi.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 28 May 2003, Craig Rodrigues wrote: > On Wed, May 28, 2003 at 08:07:24PM -0400, Daniel Eischen wrote: > > > I have some third party C++ code which tries to implements this function: > > > > > > static unsigned long > > > idFunction() > > > { > > > #ifdef _WIN32 > > > return static_cast<unsigned long>(GetCurrentThreadId()); > > > #else > > > return static_cast<unsigned long>(pthread_self()); > > > #endif > > > } > > > > > > > > > This code does not compile on FreeBSD-CURRENT: > > > > > > OpenSSLPluginI.cpp: In function `long unsigned int idFunction()': > > > OpenSSLPluginI.cpp:151: invalid static_cast from type `pthread*' to type `long > > > unsigned int' > > > > I don't know C++ well (much at all). What does static_cast do? > > > static_cast, unlike C style casts, have restrictions which can > result in compile-time errors. > > The full definition of static_cast is here: > http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.static.cast > > A static_cast *cannot* convert a pointer type to an integral type > (unsigned long), and will result in a compile time error. To quote M. Warner Losh in a very recent thread ;-), "you lose". > > The error message makes it look as if you are converting > > a "pthread" * to "long unsigned int". Don't you just > > want "unsigned long" instead? > > This function needs to return a unique numeric identifier > based on a thread. pthread_self() returns a type of pthread_t. > On Linux (where this code was written), pthread_t is: > > typedef unsigned long int pthread_t; > > So that is why this 3rd party code compiles and works fine on Linux. > > Obviously, this is not true on FreeBSD. The fact that > this code relies on pthread_t being an integer type is bogus, > but that's what you get for using 3rd party code. :) Make an external function (in C) that converts the pthread_t for you and use that to convert it. This is a standards question, but is there a reason why pthread_t can't be a pointer? We can always change the thread libraries, but that would be a version bump and doesn't help you right now. -- Dan Eischen
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.4.10.10305282038470.5688-100000>