Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #122569

Re: fun with a mutex...

From cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups comp.lang.c++
Subject Re: fun with a mutex...
Date 2025-08-18 20:44 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <10803bq$h40$4@reader1.panix.com> (permalink)
References <106cc2f$31ti3$1@dont-email.me> <107ojfk$1e84t$2@dont-email.me> <1080292$h40$2@reader1.panix.com> <108031c$3bdl8$2@dont-email.me>

Show all headers | View raw


In article <108031c$3bdl8$2@dont-email.me>,
Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>On 8/18/2025 1:26 PM, Dan Cross wrote:
>> In article <107ojfk$1e84t$2@dont-email.me>,
>> Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 8/13/2025 4:41 AM, Dan Cross wrote:
>>>> In article <107dkai$2rg26$2@dont-email.me>,
>>>> Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>>>> On 8/9/2025 8:08 PM, Dan Cross wrote:
>>>>>> In article <10781l0$1eamj$2@dont-email.me>,
>>>>>> Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>>>>>> On 8/9/2025 9:08 AM, Dan Cross wrote:
>>>>>>>> In article <10775om$17ms4$1@raubtier-asyl.eternal-september.org>,
>>>>>>>> Bonita Montero  <Bonita.Montero@gmail.com> wrote:
>>>>>>>>> Am 09.08.2025 um 11:17 schrieb Dan Cross:
>>>>>>>>>
>>>>>>>>>> People have written code that tries to take a lock, and if that
>>>>>>>>>> fails, does something else, for decades.  The idea is neither
>>>>>>>>>> new, nor terribly interesting.  Sorry.
>>>>>>>>>
>>>>>>>>> I think this pattern is extremely rare because the task that needs to
>>>>>>>>> be done elsewhere must be able to be omitted completely if necessary,
>>>>>>>>> and if not, it must be short enough that it doesn't interfere with
>>>>>>>>> waiting for the mutex.
>>>>>>>>
>>>>>>>> Yeah.  That's why I mentioned concentrating on the specific
>>>>>>>> critical section the mutex is protecting, not the locking
>>>>>>>> protocol.  Where this has any utility at all, it's in places
>>>>>>>> where the "something else" is well-known, which implies it being
>>>>>>>> a function of the critsec.  It's going to be things like, "if I
>>>>>>>> fail to acquire the scheduler lock, then increment this per-core
>>>>>>>> atomic counter that's measuring contention...." sort of thing.
>>>>>>>
>>>>>>> More grasping at straws and/or trying to breath underwater without scuba
>>>>>>> gear... The other work is very specialized. It can be as simple as
>>>>>>> processing a per-thread local queue or something. Just something that
>>>>>>> can act like a backoff that does real work in a case of contention. Sigh.
>>>>>>
>>>>>> That's the sort of thing where you cannot know what's on that
>>>>>> "per-thread local queue" that makes your scheme fail.
>>>>>
>>>>> Well, that per-thread local queue can be for organization, maintenance,
>>>>> sending the results to a "logic" thread, ect. Think of a server io
>>>>> thread that takes the io completions and organizes them into local
>>>>> queues. These io threads do not call into user logic, or processing wrt
>>>>> say, parsing HTTP, or some protocol. So, some work that can be used
>>>>> after a try_lock fails can be to push the local queues to the logic threads.
>>>>
>>>> This, yet again, seems to be where you don't understand the
>>>> issue at stake here.
>>>>
>>>> You can _probably_ find _any number of things_ that you can do
>>>> while looping over `try_lock`.  I don't care about the specific
>>>> examples: they obviously exist.
>>>>
>>>> But what you _cannot_ do, and what makes this sort of scheme
>>>> fail, is know that _all_ such work is safe.  Unless you can wave
>>>> a magic wand and solve the halting problem, you won't be able to
>>>> know that some jerk won't send you "work" that amounts to a
>>>> non-terminating infinite loop.  You won't be able to know that
>>>> some other jerk hasn't sent you "work" that causes you to
>>>> deadlock trying to take a mutex you already hold.  Etc.
>>>>
>>>> If you _ever_ open yourself up to accepting "work" on some kind
>>>> of queue, where the types of all work that can possibly be sent
>>>> to you is not known in advance _in some way that can be enforced
>>>> in code_, then your scheme fails.  And if you do have some way
>>>> to enforce those constraints, then the scheme is neither novel
>>>> nor particularly interesting.
>>>>
>>>>>> If your lock is contended, you could just sleep on it, and let
>>>>>> the kernel schedule something else instead.  There's no value to
>>>>>> keeping your thread "hot" unless you've got a profile showing
>>>>>> that it's an issue.
>>>>>
>>>>> But why sleep when the thread can do some real work?
>>>>
>>>> Because the OS (or even a user-level thread scheduler, or
>>>> language runtime, or whatever) can find work to usefully do on
>>>> that CPU, _without_ the downsides mentioned above.  So you lose
>>>> nothing but gain correctness and stability.
>>>>
>>>>> Argh. Grasping
>>>>> again. Feels like I am drowning. Shit! ;^o
>>>>
>>>> Perhaps stop doing that, then.  It's clear you are not going to
>>>> find a way to make whatever partially developed idea you have
>>>> work.
>>>
>>> It should be okay if the other work is okay with it?
>> 
>> Again: you have no way of knowing that "the other work is okay
>> with it".
>
>Sure you can in highly controlled scenarios? A programmer can say, this 
>type of work can be done in this condition because the programmer knows 
>what the work consists of... I listed a few of them before. I am not 
>going for some sort of general case type of thing.
>
>
>>>> It's ok to be wrong.  It's less ok to keep trying to convince
>>>> others that you are right.
>>>
>>> It just seems to be okay, well, after a try_lock fails try to do
>>> something that is _compatible_ instead of something like an adaptive
>>> mutex? Shit. Yikes!
>>>
>>> There I go again. Sorry.
>>>
>>> [...]
>> 
>> Please stop.
>
>It sure seems like it cannot be 100% wrong all the time. Having trouble 
>seeing that.

It's already in the above quoted text.  The relevant section is
here:

|You can _probably_ find _any number of things_ that you can do
|while looping over `try_lock`.  I don't care about the specific
|examples: they obviously exist.
|
|But what you _cannot_ do, and what makes this sort of scheme
|fail, is know that _all_ such work is safe.  Unless you can wave
|a magic wand and solve the halting problem, you won't be able to
|know that some jerk won't send you "work" that amounts to a
|non-terminating infinite loop.  You won't be able to know that
|some other jerk hasn't sent you "work" that causes you to
|deadlock trying to take a mutex you already hold.  Etc.
|
|If you _ever_ open yourself up to accepting "work" on some kind
|of queue, where the types of all work that can possibly be sent
|to you is not known in advance _in some way that can be enforced
|in code_, then your scheme fails.  And if you do have some way
|to enforce those constraints, then the scheme is neither novel
|nor particularly interesting.

Just saying, "yay! I can do something while spinning on a lock!"
is so vaccuously obvious that it's simply uninteresting; people
have done such things for many, many years.  Your error is in
trying to extrapolate that to "other" work.

	- Dan C.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-07-29 22:54 -0700
  Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-02 12:58 -0700
    Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-02 13:00 -0700
    Re: fun with a mutex... Muttley@DastardlyHQ.org - 2025-08-04 08:48 +0000
      Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-04 12:34 -0700
        Re: fun with a mutex... Muttley@DastardlyHQ.org - 2025-08-05 07:27 +0000
          Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-05 16:10 -0700
            Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-06 00:38 +0000
              Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-05 19:14 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-06 11:29 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-06 15:43 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-08 01:41 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 20:52 -0700
              Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-05 19:16 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-06 12:04 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-06 15:47 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-08 01:49 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 20:59 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-09 09:17 +0000
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-09 11:52 +0200
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-09 16:08 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-09 10:48 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-10 03:08 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-11 13:37 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-13 11:41 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-15 17:28 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 20:25 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 13:34 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 20:39 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 14:06 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 14:28 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 14:33 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 22:56 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 16:09 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 23:44 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 16:55 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 23:57 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-19 13:25 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-15 17:30 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 20:26 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 13:39 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-18 20:44 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 14:08 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 16:11 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-18 16:20 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-11 11:04 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-11 13:30 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-12 10:23 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-12 01:31 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-12 10:39 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-12 01:45 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-12 10:56 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-12 02:14 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-12 15:08 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-15 21:36 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-16 10:13 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-16 01:56 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-16 11:05 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-16 13:31 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-17 14:50 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-17 13:43 -0700
                Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-13 11:45 +0000
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-13 19:07 +0200
                Re: fun with a mutex... scott@slp53.sl.home (Scott Lurndal) - 2025-08-13 17:31 +0000
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-15 10:00 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-13 12:41 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-09 10:44 -0700
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-09 11:00 -0700
        Re: fun with a mutex... "Fred. Zwarts" <F.Zwarts@HetNet.nl> - 2025-08-05 10:27 +0200
          Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-05 16:07 -0700
            Re: fun with a mutex... "Fred. Zwarts" <F.Zwarts@HetNet.nl> - 2025-08-06 10:56 +0200
              Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-06 15:36 -0700
      Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-04 12:38 -0700
        Re: fun with a mutex... Muttley@DastardlyHQ.org - 2025-08-05 07:29 +0000
          Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-05 16:08 -0700
  Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-06 15:51 +0200
    Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-06 15:34 -0700
      Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-07 06:44 +0200
        Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 12:17 -0700
          Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-07 21:51 +0200
            Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 12:52 -0700
              Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-16 11:21 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-16 13:22 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-17 03:59 +0200
                Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-16 23:53 -0700
                Re: fun with a mutex... Bonita Montero <Bonita.Montero@gmail.com> - 2025-08-17 10:37 +0200
        Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 12:32 -0700
      Re: fun with a mutex... cross@spitfire.i.gajendra.net (Dan Cross) - 2025-08-08 01:51 +0000
        Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 21:01 -0700
          Re: fun with a mutex... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-08-07 21:08 -0700

csiph-web