public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Ross Johnson <Ross.Johnson@homemail.com.au>
To: pthreads-win32@sourceware.org
Subject: Re: Pthread-win32 races?
Date: Wed, 13 Dec 2006 08:39:00 -0000	[thread overview]
Message-ID: <457FBC2B.8090301@homemail.com.au> (raw)
In-Reply-To: <321e820c0612120427t6cff8a59h35eb5fde17a7506f@mail.gmail.com>

Hi Sergey,

The library is working correctly since sem_destroy() is returning the 
error EBUSY as required and documented at:

http://sourceware.org/pthreads-win32/manual/sem_init.html

This is also in accordance with the Single Unix Specification. If it was 
hanging your program rather than returning the error then that would be 
a problem.

By the way, in your sample code you don't check the return code from the 
sem_post(), but the semaphore could already be destroyed at that point. 
It would be better in this and similar cases to call sem_destroy() after 
the call to pthread_join(), or at least after you can guarantee that the 
semaphore is no longer required by any child threads.

A sem_t "handle" is not required to be unique in time, so it's possible 
to destroy a semaphore and init a new one having another purpose 
altogether, which then by chance occupies the same physical memory 
location, i.e. has the same "handle" (in pthreads-win32 this is just the 
pointer to the struct in memory), so a sema op somewhere may not fail 
even though, logically, it is no longer accessing the semaphore it 
should be, and the application may now be mysteriously badly behaved and 
difficult to debug.

Regards.
Ross

Sergey Fokin wrote:
> Hi all.
>
> I have some peculiarities with pthread-win32 and suppose there's a bug
> in library.
>
> Here's my example code:
>
> #include <pthread.h>
> #include <semaphore.h>
> #include <stdio.h>
> #include <assert.h>
>
> void * thr(void * arg)
> {
>  sem_post((sem_t*)arg);
>  return 0;
> }
>
> int main()
> {
>    sem_t sem;
>    int error = 0;
>    error = sem_init(&sem, 0, 0); // OK
>    assert(!error);
>
>    pthread_t thread;
>    error = pthread_create(&thread, 0, thr, &sem); // OK
>    assert(!error);
>
>    sem_wait(&sem);
>    error = sem_destroy(&sem);
>    if (error != 0)
>    {
>        error = errno; // errno == 16 (EBUSY)
>        printf("errno = %d\n", error);
>    }
>
>    pthread_join(thread, 0);
>
>    return error;
> }
>
>
> So, here we have error 16 (0x10) in almost all runs, independently on
> compile options (at least i couldn't find working combination).
> Compiler is msvc 7.1 sp1.
>
> I've read about some troubles with it in BUGS file, but first, this
> one is unrelated to those, as I can see, second all tests from `tests'
> directory run with no errors, but this one fails even with same
> compile options.
>
> When I've tried to debug, it turned out that when main() is executing
> sem_destroy(), child thread is still in sem_post(). But I couldn't
> find out what's going on there and supposed this is some kind of race,
> this is why subject is about races.
>
> Google didn't find similar issues about pthread-win32 library.
>
> Really hope, this is my fault, but I have no idea where I'm wrong.
>
> Thanks in advance.
>
> PS I'll also try gcc for win32 and msvc8.0 later - I don't have them
> on this computer.
>

  reply	other threads:[~2006-12-13  8:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-12 12:27 Sergey Fokin
2006-12-13  8:39 ` Ross Johnson [this message]
2006-12-13  9:26   ` Sergey Fokin
2006-12-13 14:26     ` Ross Johnson
2006-12-20  2:08 ` Ross Johnson
2006-12-12 20:12 Ye Liu

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=457FBC2B.8090301@homemail.com.au \
    --to=ross.johnson@homemail.com.au \
    --cc=pthreads-win32@sourceware.org \
    /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).