public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Ger Hobbelt <ger@hobbelt.com>
To: markus.forrer@gmx.ch
Cc: pthreads-win32@sourceware.org
Subject: Re: Problems with conditions
Date: Tue, 30 Mar 2021 09:15:34 +0200	[thread overview]
Message-ID: <CAFP60fop6zg36PTGoe_-b7xnkgZekaNJASe3mYksMZmgDuss6Q@mail.gmail.com> (raw)
In-Reply-To: <trinity-fcaf2ac8-7b07-45d4-a019-052fd50dfabb-1617020398606@3c-app-gmx-bap72>

See
https://stackoverflow.com/questions/16522858/understanding-of-pthread-cond-wait-and-pthread-cond-signal/16524148,
also read ALL comments there as some details get explained further.

Compare your mutex lock.. Unlock code with the sample code in the answer
there: your code has "nothing to do that requires..." so do not need to
wait.

On Mon, Mar 29, 2021, 14:20 Markus Forrer via Pthreads-win32 <
pthreads-win32@sourceware.org> wrote:

> I have re-implemented the C++ class OSEvent for a Windows simulation that
> exists for a Linux platform. Thereby I have the problem that the function
> pthread_cond_wait() does not wait until pthread_cond_signal() is called.
> Something I have misunderstood.
>
> Many thanks for any hints!
>
>
> OSEvent::OSEvent()
> {
>     pthread_condattr_init(&_conditionAttribute);
>     pthread_cond_init(&_condition, &_conditionAttribute);
>     pthread_mutexattr_init(&_mutexAttribute);
>     pthread_mutexattr_settype(&_mutexAttribute, PTHREAD_MUTEX_ERRORCHECK);
>     pthread_mutex_init(&_mutex, &_mutexAttribute);
> }
>
> OSEvent::~OSEvent()
> {
>     pthread_cond_destroy(&_condition);
>     pthread_condattr_destroy(&_conditionAttribute);
>     pthread_mutex_destroy(&_mutex);
>     pthread_mutexattr_destroy(&_mutexAttribute);
> }
>
> void OSEvent::signal()
> {
>     pthread_mutex_lock(&_mutex);
>     pthread_cond_signal(&_condition);
>     pthread_mutex_unlock(&_mutex);
> }
>
> OSEventError OSEvent::timedwait(uint32_t timeout)
> {
>     struct timespec timeoutTime;
>     timeoutTime.tv_sec = timeout / 1000;
>     timeoutTime.tv_nsec = 1000000 * (timeout % 1000);
>     OSEventError success = OSEventError::NoError;
>     pthread_mutex_lock(&_mutex);
>
>     // Timeout forever
>     if (timeout == 0)
>     {
>         if (pthread_cond_wait(&_condition, &_mutex) != 0)
>         {
>             success = OSEventError::Timeout;
>         }
>     }
>
>     // Timeout time
>     else
>     {
>         if (pthread_cond_timedwait(&_condition, &_mutex, &timeoutTime) !=
> 0)
>         {
>             success = OSEventError::Timeout;
>         }
>     }
>
>     pthread_mutex_unlock(&_mutex);
>     return success;
> }
>
>

  reply	other threads:[~2021-03-30  7:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-29 12:19 markus.forrer
2021-03-30  7:15 ` Ger Hobbelt [this message]
2021-03-30 15:37   ` Aw: " markus.forrer
2021-03-31  1:30     ` ross.johnson
2021-03-31  4:43       ` Markus Forrer

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=CAFP60fop6zg36PTGoe_-b7xnkgZekaNJASe3mYksMZmgDuss6Q@mail.gmail.com \
    --to=ger@hobbelt.com \
    --cc=markus.forrer@gmx.ch \
    --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).