public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: "Joël Krähemann" <jkraehemann@gmail.com>
To: Sebastian Huber <sebastian.huber@embedded-brains.de>
Cc: libc-help@sourceware.org
Subject: Re: pthread_mutex_timedlock() vs. clock_settime()
Date: Mon, 1 Aug 2022 16:44:00 +0200	[thread overview]
Message-ID: <CA+Owze6hKccjT6u_b7=P36wdeAvev+POnacj6hgBtR7_smYdvg@mail.gmail.com> (raw)
In-Reply-To: <CA+Owze46b7jX5pox2zkq6Y2b2HHF+qRofwvc8t0hR0W0f5769g@mail.gmail.com>

Hi Sebastian,

Since you are new to parallelism, make sure you know about cache
coherency and its implications.

https://www.intel.com/content/dam/support/us/en/documents/processors/pentium4/sb/25366821.pdf

There are different synchronization scenarios.

1. hit and run, you can use ordinary boolean variables in your conditional lock
2. continuously synchronization within a loop, use atomic boolean
variables in your conditional lock

The second case is different. You probably want to take advantage of
atomic operations memory barrier behaviour.

regards,
Joël

On Mon, Aug 1, 2022 at 1:59 PM Joël Krähemann <jkraehemann@gmail.com> wrote:
>
> Hi,
>
> I co authored an article, check the section about working with parallelism:
>
> https://opensource.com/article/21/11/community-code-stories
>
> regards,
> Joël
>
> On Mon, Aug 1, 2022 at 1:51 PM Joël Krähemann <jkraehemann@gmail.com> wrote:
> >
> > Hi Sebastian,
> >
> > You should inform yourself better about the topic. Maybe read a manual
> > about conditional locks. Since you are doing it wrong.
> >
> > All conditional locks take 2 condition variables, in order to prevent
> > race-condition.
> >
> > See for example here:
> >
> > https://git.savannah.nongnu.org/cgit/gsequencer.git/tree/ags/thread/ags_thread.c?id=393da4d9fdc6c50ef3ab8eab27b562c85bd57664#n2002
> >
> > Or check this for condition:
> >
> > ~~~~
> >
> > volatile int is_wait;
> > volatile int is_done;
> >
> > static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER:
> > static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
> >
> > pthread_mutex_lock(&mutex);
> >
> > if(g_atomic_int_get(&is_wait) &&
> >   !g_atomic_int_get(&is_done)){
> >   g_atomic_int_set(&is_wait, TRUE);
> >
> >   while(g_atomic_int_get(&is_wait) &&
> >     !g_atomic_int_get(&is_done)){
> >     pthread_cond_wait(&cond);
> >   }
> > }
> >
> > pthread_mutex_lock(&mutex);
> >
> >
> > ~~~~
> >
> > Then later signal:
> >
> > ~~~~
> >
> > g_mutex_lock(&mutex);
> >
> > g_atomic_int_set(&is_done, TRUE);
> >
> > if(g_atomic_int_get(&is_wait)){
> >     pthread_cond_signal(&cond);
> > }
> >
> > g_mutex_unlock(&mutex);
> >
> > ~~~~
> >
> > regards,
> > Joël
> >
> > On Mon, Aug 1, 2022 at 11:51 AM Joël Krähemann <jkraehemann@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > Sorry, you are right. Might be your conditional lock is buggy. I think
> > > of a race-condition ending in a dead-lock, because you don't do any
> > > initial sync.
> > >
> > > ^^ this is really probable, but let me check later.
> > >
> > > regards,
> > > Joël
> > >
> > >
> > > On Mon, Aug 1, 2022 at 10:36 AM Sebastian Huber
> > > <sebastian.huber@embedded-brains.de> wrote:
> > > >
> > > > On 01/08/2022 10:28, Joël Krähemann wrote:
> > > > > pthread_mutex_t mtx of struct test_context.
> > > > >
> > > > > You lock it in line 503 first, then you call test_timeout_finite(),
> > > > > which calls event_post() in run() second lock.
> > > >
> > > > the event_post() and event_get() use their own data structures with
> > > > dedicated mutexes and condition variables:
> > > >
> > > > typedef struct {
> > > >    int value;
> > > >    pthread_mutex_t mtx;
> > > >    pthread_cond_t cnd;
> > > > } event;
> > > >
> > > > --
> > > > embedded brains GmbH
> > > > Herr Sebastian HUBER
> > > > Dornierstr. 4
> > > > 82178 Puchheim
> > > > Germany
> > > > email: sebastian.huber@embedded-brains.de
> > > > phone: +49-89-18 94 741 - 16
> > > > fax:   +49-89-18 94 741 - 08
> > > >
> > > > Registergericht: Amtsgericht München
> > > > Registernummer: HRB 157899
> > > > Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
> > > > Unsere Datenschutzerklärung finden Sie hier:
> > > > https://embedded-brains.de/datenschutzerklaerung/

  reply	other threads:[~2022-08-01 14:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02  8:43 Sebastian Huber
2022-08-01  7:10 ` Joël Krähemann
2022-08-01  7:41   ` Joël Krähemann
2022-08-03 23:44   ` Sebastian Huber
2022-08-01  8:28     ` Joël Krähemann
2022-08-01  8:36       ` Sebastian Huber
2022-08-01  9:51         ` Joël Krähemann
2022-08-01 11:51           ` Joël Krähemann
2022-08-01 11:59             ` Joël Krähemann
2022-08-01 14:44               ` Joël Krähemann [this message]
2022-08-01 16:31             ` Sebastian Huber
2022-08-01 20:11               ` Joël Krähemann
2022-08-01 22:05                 ` Joël Krähemann

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='CA+Owze6hKccjT6u_b7=P36wdeAvev+POnacj6hgBtR7_smYdvg@mail.gmail.com' \
    --to=jkraehemann@gmail.com \
    --cc=libc-help@sourceware.org \
    --cc=sebastian.huber@embedded-brains.de \
    /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).