This is the mail archive of the
mailing list for the pthreas-win32 project.
Re: How is pthread_self() implemented?
- From: Ross Johnson <rpj at ise dot canberra dot edu dot au>
- To: Chris Seaton <chris at chrisseaton dot com>
- Cc: pthreads-win32 at sources dot redhat dot com
- Date: Mon, 01 Sep 2003 14:32:16 +1000
- Subject: Re: How is pthread_self() implemented?
- References: <1062352346.4373.7.camel@william>
- Reply-to: rpj at callisto dot canberra dot edu dot au
Chris Seaton wrote:
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.
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
Originally I called GetCurrentThread(), but that returns a pseudo
handle, so I call DuplicateHandle(), as this pthreads-win32 library
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?
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