From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32743 invoked by alias); 15 May 2006 14:24:46 -0000 Received: (qmail 32692 invoked by uid 22791); 15 May 2006 14:24:45 -0000 X-Spam-Check-By: sourceware.org Received: from canyonero.dot.net.au (HELO canyonero.dot.net.au) (202.147.68.14) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 15 May 2006 14:24:43 +0000 Received: from [203.129.40.123] (helo=[203.129.40.123]) by canyonero.dot.net.au with esmtp (Exim 3.35 #1 (Debian)) id 1Ffdzy-0006NP-00 for ; Tue, 16 May 2006 00:24:38 +1000 Message-ID: <44688F28.2020305@callisto.canberra.edu.au> Date: Mon, 15 May 2006 14:24:00 -0000 From: Ross Johnson User-Agent: Mozilla Thunderbird 1.0.7-1.1.fc3 (X11/20050929) MIME-Version: 1.0 To: pthreads-win32@sources.redhat.com Subject: Re: Using pthread_t as a key in a map References: <553911630605091355q64a6eec6i76cadef9b9efa0e@mail.gmail.com> <4467E5FA.3020300@ecosm.com> In-Reply-To: <4467E5FA.3020300@ecosm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact pthreads-win32-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sourceware.org X-SW-Source: 2006/txt/msg00022.txt.bz2 A reasonably portable solution to this came up the other day (privately) in relation to using pthreads-win32 pthread_t with openssl. It is the following: #define PT(t) (*((unsigned long *)&(t))) pthread_t thrA, thrB; ... PT(thrA) < PT(thrB) etc. Although this still relies on the pointer component being at offset 0 inside the pthread_t struct (which was made this way deliberately so probably won't change in the future), it does avoid having to explicitly use or know about ptw32_handle_t. You still lose the handle uniqueness "guarantee" that the pthread_t struct provides though, so that you then need to take extra care that the threads that you do this with don't detach without you realising it. By the way, the pthread_t in pthreads-win32 is POSIX compliant. POSIX doesn't require pthread_t to be a scalar type, and in fact all but requires that POSIX compliant applications not presume the type of pthread_t. Ross Will Bryant wrote: >Hi Paolo, > >Bear in mind that pthreads-win32 is written in C, and operator >overloading is a C++ feature, so adding that would prevent >pthreads-win32 from compiling with C apps. > >One alternative is to make a custom comparator type and use that in the >map declaration - or you could even simply move those operator overloads >to your own units (they don't have to be defined where the type being >compared is declared, as long as they've visible at the point where >they're used - ie. your map declaration). > >But bear in mind that in any case, making use of the ptw32_handle_t type >makes your code nonportable, and since portability is generally the >reason one is using pthreads-win32 in the first place, this is perhaps >not the best design for general use. > >Will > > >Paolo Brandoli wrote: > > >>I have a source code that uses the pthread_t as a key in a std::map. >>Because pthread-win32 defines pthread_t as a structure, the >>compilation fails. >> >>I added the following lines in my pthread.h header in order to allow >>the usage of pthread_t in the map: >> >>bool operator < (const ptw32_handle_t& left, const ptw32_handle_t& right) >>{ >> return left.p < right.p; >>} >> >>bool operator > (const ptw32_handle_t& left, const ptw32_handle_t& right) >>{ >> return left.p > right.p; >>} >> >>Bye >>Paolo Brandoli >>http://www.puntoexe.com >> >> >> > > > >