From owner-freebsd-threads@FreeBSD.ORG Wed May 28 17:46:28 2003 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2CFE037B401 for ; Wed, 28 May 2003 17:46:27 -0700 (PDT) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4298743F75 for ; Wed, 28 May 2003 17:46:27 -0700 (PDT) (envelope-from eischen@pcnet.com) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mail.pcnet.com (8.12.8/8.12.1) with ESMTP id h4T0kQnb006664; Wed, 28 May 2003 20:46:26 -0400 (EDT) Date: Wed, 28 May 2003 20:46:26 -0400 (EDT) From: Daniel Eischen To: Craig Rodrigues In-Reply-To: <20030529003416.GA3712@attbi.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-threads@freebsd.org Subject: Re: Question about OpenSSL id_function() and pthreads X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 May 2003 00:46:28 -0000 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(GetCurrentThreadId()); > > > #else > > > return static_cast(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