Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 May 2003 20:34:16 -0400
From:      Craig Rodrigues <rodrigc@attbi.com>
To:        freebsd-threads@freebsd.org
Subject:   Re: Question about OpenSSL id_function() and pthreads
Message-ID:  <20030529003416.GA3712@attbi.com>
In-Reply-To: <Pine.GSO.4.10.10305282000170.133-100000@pcnet5.pcnet.com>
References:  <20030528235120.GA3481@attbi.com> <Pine.GSO.4.10.10305282000170.133-100000@pcnet5.pcnet.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@attbi.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030529003416.GA3712>