From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5692 invoked by alias); 1 Sep 2003 04:33:42 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 5684 invoked from network); 1 Sep 2003 04:33:40 -0000 Received: from unknown (HELO real.ise.canberra.edu.au) (137.92.140.34) by sources.redhat.com with SMTP; 1 Sep 2003 04:33:40 -0000 Received: from ise.canberra.edu.au (special.ise.canberra.edu.au [137.92.140.39]) by real.ise.canberra.edu.au (8.11.6/8.11.6) with ESMTP id h814Wfi04769; Mon, 1 Sep 2003 14:32:57 +1000 Message-ID: <3F52CBD0.2070205@ise.canberra.edu.au> Date: Mon, 01 Sep 2003 04:33:00 -0000 From: Ross Johnson Reply-To: rpj@callisto.canberra.edu.au User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Chris Seaton CC: pthreads-win32@sources.redhat.com Subject: Re: How is pthread_self() implemented? References: <1062352346.4373.7.camel@william> In-Reply-To: <1062352346.4373.7.camel@william> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2003/txt/msg00083.txt.bz2 Chris Seaton wrote: >I'm writing my own (very lightweight) threading library for Windows and >POSIX threads. There are reasons why I can't use pthreads-win32, but >they aren't important here. I'm currently implementing a ThisThread() >routine. With POSIX threads I simply call pthread_self(), but I'm stuck >for Windows. > >Originally I called GetCurrentThread(), but that returns a pseudo >handle, so I call DuplicateHandle(), as this pthreads-win32 library >uses. > >However, DuplicateHandle() creates a new handle every time it is called, >so I can't compare them. Basically > >ThisThread() != ThisThread() > >How does the pthreads-win32 library solve this problem? > > Pthreads-win32 keeps a POSIX thread struct in which it stores the handle returned by the original Win32 CreateThread(). The pointer to the POSIX struct is kept in TLS (TSD) so that other pthread library functions can refer to that struct. John Bossom's original design also allows for pre-existing Win32 threads to use any POSIX routines, and therefore fully interact with POSIX threads, by creating a one-time-only on-the-fly detached POSIX thread handle for the Win32 thread. In this case, the library uses DuplicateHandle(), which is sufficient for our purposes. This is the logic you'll see in pthread_self.c at the following URL (all one line): http://sources.redhat.com/cgi-bin/cvsweb.cgi/pthreads/pthread_self.c?rev=1.2&content-type=text/x-cvsweb-markup&cvsroot=pthreads-win32 Regards. Ross