Question about OpenSSL id_function() and pthreads

Craig Rodrigues rodrigc at attbi.com
Wed May 28 17:34:14 PDT 2003


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.


> 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. :)

So how do I solve this problem on FreeBSD?

-- 
Craig Rodrigues        
http://home.attbi.com/~rodrigc
rodrigc at attbi.com


More information about the freebsd-threads mailing list