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


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

Re: a try_lock pattern...

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c++
Subject Re: a try_lock pattern...
Date 2024-09-28 12:18 -0700
Organization A noiseless patient Spider
Message-ID <vd9kph$1c8ti$1@dont-email.me> (permalink)
References <vd8an7$15ff9$1@dont-email.me> <vd8cmd$159kt$3@dont-email.me>

Show all headers | View raw


On 9/28/2024 12:53 AM, Paavo Helde wrote:
> On 28.09.2024 10:20, Chris M. Thomasson wrote:
>> This is just a quick test of my try_lock pattern thing. ct_shared::m_b 
>> counts how many times extra work was able to be done instead of 
>> spinning on the ct_shared::m_lock_a mutex. Take careful notice of the 
>> ct_shared::work_a_lock() function.
>>
>> Can you test it out? A large number for ct_shared::m_b is ideal. It 
>> means extra work was being done. Avoids spinning in an interesting way.
>>
>> my crude code, can you compile and run it okay? Thanks:
>> _________________________

[...]

>> _________________________
>>
>>
>> What results do you get for ct_shared::m_b? Here is a result I get:
>> _____________________________
>> ct try_lock pattern Test...
>>
>> launching threads...
>> processing...
>>
>> shared.m_a = 0
>> shared.m_b = 8664093
>>
>> ct try_lock pattern Test completed.
>> _____________________________
>>
>> Well, shared.m_b is a high number... That is a lot potential kernel 
>> calls that were skipped in favor of real work.
>>
>> Any thoughts? Thanks.
> 
> Seems to compile and work fine with VS2022 on Windows x86_64

Nice! Thanks for taking the time to give it a go Paavo. Basically, the 
main "pattern" is in the ct_shared::work_a_lock() function:
_____________________
void work_a_lock()
{
     while (! m_lock_a.try_lock())
     {
         if (! work_b_try(1))
         {
             m_lock_a.lock();
             break;
         }
     }
}
_____________________

If the m_lock_a.try_lock() fails, we know the lock is contended. So, why 
not try to do some other work ala work_b_try(1)? If the work_b got to do 
something useful, we do not need to block for m_lock_a. We can _try_ to 
lock it again. If work_b_try fails to do any work, we simply commit and 
block on m_lock_a via m_lock_a.lock() instead of try_lock().

This pattern is interesting to me.


> ct try_lock pattern Test...
> 
> launching threads...
> processing...
> 
> shared.m_a = 0
> shared.m_b = 6976047
> 
> ct try_lock pattern Test completed.
> 
> 

So, real work wrt work_b was performed 6976047 times in this run. This 
is good because it means actual work was done while m_lock_a was contended.

Make any sense to you? Thanks again Paavo.

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


Thread

a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 00:20 -0700
  Re: a try_lock pattern... Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-28 10:53 +0300
    Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 12:18 -0700
      Re: a try_lock pattern... scott@slp53.sl.home (Scott Lurndal) - 2024-09-29 18:03 +0000
        Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 20:15 +0200
          Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 13:18 -0700
            Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:53 +0200
              Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 16:41 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 06:45 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:44 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 21:40 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:48 -0700
        Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 12:55 -0700
  Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-28 10:40 +0200
    Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 12:55 -0700
      Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 16:30 -0700
        Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 08:15 +0200
          Re: a try_lock pattern... Muttley@dastardlyhq.com - 2024-09-29 09:01 +0000
            Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 12:54 +0200
              Re: a try_lock pattern... Muttley@dastardlyhq.com - 2024-09-29 14:56 +0000
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 17:20 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 13:24 -0700
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 14:03 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:51 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:34 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 00:58 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 14:45 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:52 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 18:11 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 06:47 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:45 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 08:02 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 16:37 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 09:17 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:49 -0700
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:37 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 01:30 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 16:37 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 01:43 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 18:20 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 03:24 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 18:25 -0700
                Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 04:13 +0200
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-02 16:57 -0700
                Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 19:53 -0700
            Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 12:58 -0700

csiph-web