public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Ross Johnson <rpj@callisto.canberra.edu.au>
To: Will Bryant <will.bryant@ecosm.com>
Cc: pthreads-win32@sources.redhat.com
Subject: Re: How is pthread_self() implemented?
Date: Mon, 01 Sep 2003 07:22:00 -0000	[thread overview]
Message-ID: <3F52F356.3080802@callisto.canberra.edu.au> (raw)
In-Reply-To: <07ea01c37043$43e02a70$2b01a8c0@moose>

Will Bryant wrote:

>>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.
>>    
>>
>
>So it is safe to use all pthreads-win32 functions from threads not created
>using the pthreads interface?  I have an application where nearly all of the
>code only needs to use standard windows threads, but there are a couple of
>condition variables that I'd like to be able to use...
>
Note that non-POSIX threads are regarded by pthreads-win32 as detached 
and deferred-cancelable initially. However, I don't think there's any 
reason a non-POSIX thread can't subsequently change it's cancelability 
type. There's no way it can make itself joinable though. Apart from 
that, just about everything should work.

In particular, for condition variables I haven't proven the following 
but I believe it should all work properly. And since this should work, 
then so should POSIX read-write locks. Note that you odn't need to 
explicitly call pthread_self() unless you need the POSIX thread handle. 
Any routine that needs it internally will cause the POSIX handle to be 
created implicitly.

// Global

pthread_t    posixGuest[MAX_GUESTS];
pthread_mutex_t    CVLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t    CV = PTHREAD_COND_INITIALIZER;
int champaigneChilled = 0;
int cakeIced = 0;
int chaos = 0;
int mayhem = 0;
long    guest = 0;

// Thread A (Win32 or POSIX thread):

    // Party guest thread
    ...
    if ((guest = InterLockedIncremement(&guest)) >= MAX_GUESTS)
        return(gateCrasher);
    posixGuest[guest] = pthread_self();    // Only needed sometimes e.g. 
see thread D
    // Mingle
    pthread_mutex_lock(&CVLock);
    pthread_cleanup_push(pthread_mutex_unlock(&CVLock));
    while ( ! (champaigneChilled && cakeIced ) ) {
        pthread_cond_wait(&CV, &CVLock);
    }
    pthread_cleanup_pop(1);

    toastBirthdaySubject();
    eatCake();
    ...

// Thread B (Win32 or POSIX thread):

    // Ice cake thread
    ...
    cakeIced = 1;
    pthread_cond_broadcast(&CV);
    ...

// Thread C (Win32 or POSIX thread):

    // Chill champaigne thread
    ...
    champaigeChilled = 1;
    pthread_cond_broadcast(&CV);
    ...

// Thread D (Win32 or POSIX thread):

    // Monitor party thread
    ...
    if (chaos && meyhem) {
        // Evict our guests
        for (i = 0; i < MAX_GUESTS && pthread_kill(posixGuest[i], 0) == 
0; i++)
            pthread_cancel(posixGuest[i]);
    }
    ...


Regards.
Ross


  reply	other threads:[~2003-09-01  7:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-31 17:52 Chris Seaton
2003-09-01  4:33 ` Ross Johnson
2003-09-01  4:41   ` Will Bryant
2003-09-01  7:22     ` Ross Johnson [this message]
2003-09-01 15:49       ` Ross Johnson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3F52F356.3080802@callisto.canberra.edu.au \
    --to=rpj@callisto.canberra.edu.au \
    --cc=pthreads-win32@sources.redhat.com \
    --cc=will.bryant@ecosm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).