• Re: Run this under Windows and Linux

    From Chris M. Thomasson@21:1/5 to Bonita Montero on Mon Oct 30 13:08:51 2023
    On 10/30/2023 12:12 AM, Bonita Montero wrote:
    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:
    [...]

    Test out the benaphore:

    https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html#Engineering1-26

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Oct 31 13:25:17 2023
    Am 30.10.2023 um 21:08 schrieb Chris M. Thomasson:

    On 10/30/2023 12:12 AM, Bonita Montero wrote:

    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:
    [...]

    Test out the benaphore: https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html#Engineering1-26

    I think this guy never heard of how a usual mutex works and re-invented
    the wheel and called it a benaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Branimir Maksimovic@21:1/5 to Bonita Montero on Tue Oct 31 17:37:04 2023
    On 2023-10-31, Bonita Montero <[email protected]> wrote:
    Am 30.10.2023 um 21:08 schrieb Chris M. Thomasson:

    On 10/30/2023 12:12 AM, Bonita Montero wrote:

    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:
    [...]

    Test out the benaphore:
    https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html#Engineering1-26

    I think this guy never heard of how a usual mutex works and re-invented
    the wheel and called it a benaphore.

    formatt elf64

    FUTEX_WAIT equ 0
    FUTEX_WAKE equ 1
    FUTEX_PRIVATE_FLAG equ 128

    public futex_acquire
    public futex_release

    futex_acquire:
    push rbx
    push r15
    ; push r10
    mov r15,rdi
    .L0:
    mov ebx,1
    xor eax,eax
    lock cmpxchg [r15],ebx
    test eax,eax
    jz .L1
    mov eax, 202
    mov rdi, r15
    mov rsi, FUTEX_WAIT or FUTEX_PRIVATE_FLAG
    mov edx, 1
    xor r10,r10
    syscall
    jmp .L0
    .L1:; pop r10
    pop r15
    pop rbx
    ret

    futex_release:
    lock and dword[rdi],0
    mov eax,202
    ; mov rdi, sema
    mov rsi, FUTEX_WAKE or FUTEX_PRIVATE_FLAG
    mov edx,1
    syscall
    ret

    ---------
    x86 mutex on linux

    --

    7-77-777, Evil Sinner! https://www.linkedin.com/in/branimir-maksimovic-6762bbaa/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Tue Oct 31 13:59:22 2023
    On 10/31/2023 5:25 AM, Bonita Montero wrote:
    Am 30.10.2023 um 21:08 schrieb Chris M. Thomasson:

    On 10/30/2023 12:12 AM, Bonita Montero wrote:

    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:
    [...]

    Test out the benaphore:
    https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html#Engineering1-26

    I think this guy never heard of how a usual mutex works and re-invented
    the wheel and called it a benaphore.


    Why do you say that?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 1 13:00:23 2023
    Am 31.10.2023 um 21:59 schrieb Chris M. Thomasson:

    On 10/31/2023 5:25 AM, Bonita Montero wrote:

    I think this guy never heard of how a usual mutex works
    and re-invented the wheel and called it a benaphore.

    Why do you say that?

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 1 13:21:17 2023
    Am 31.10.2023 um 21:59 schrieb Chris M. Thomasson:

    Why do you say that?

    The article you mentioned is also discussed here: https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss
    The first replier says the same I said: its a normal mutex.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 1 19:28:28 2023
    Am 01.11.2023 um 19:24 schrieb Bonita Montero:

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.

    I found that LOCK XADD was introduced 1990 with the i486.
    I think the designers of this instruction had this specific
    kind of mutex in mind.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 1 19:24:06 2023
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Wed Nov 1 19:04:54 2023
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 19:24 schrieb Bonita Montero:

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.

    I found that LOCK XADD was introduced 1990 with the i486.

    Multiprocessor computer systems existed long before the i486;
    nothing the 486 did was new.

    Burroughs mainframes had mutexes and condition variables in the
    1970s (during which time Dijkstra was a Burroughs fellow).

    A great deal of research on synchronization was done in the
    60s and 1970s, including work by Lamport, Hoare, Dijkstra
    and others.

    I always found Hoare's Co-operating Sequential Processes interesting,
    here's his book (not pirated):

    http://www.usingcsp.com/

    The book is based on his 197x paper we studied in school.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 1 20:11:32 2023
    Am 01.11.2023 um 20:04 schrieb Scott Lurndal:

    Bonita Montero <[email protected]> writes:

    I found that LOCK XADD was introduced 1990 with the i486.

    Multiprocessor computer systems existed long before the i486;
    nothing the 486 did was new.

    I treaded the indroduction of LOCK XADD just as a hint that sth.
    like this benaphore was thoguht before 1996.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 1 12:59:17 2023
    On 11/1/2023 5:21 AM, Bonita Montero wrote:
    Am 31.10.2023 um 21:59 schrieb Chris M. Thomasson:

    Why do you say that?

    The article you mentioned is also discussed here: https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss
    The first replier says the same I said: its a normal mutex.


    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 1 12:58:30 2023
    On 11/1/2023 5:00 AM, Bonita Montero wrote:
    Am 31.10.2023 um 21:59 schrieb Chris M. Thomasson:

    On 10/31/2023 5:25 AM, Bonita Montero wrote:

    I think this guy never heard of how a usual mutex works
    and re-invented the wheel and called it a benaphore.

    Why do you say that?

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.


    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 1 13:02:43 2023
    On 11/1/2023 12:11 PM, Bonita Montero wrote:
    Am 01.11.2023 um 20:04 schrieb Scott Lurndal:

    Bonita Montero <[email protected]> writes:

    I found that LOCK XADD was introduced 1990 with the i486.

    Multiprocessor computer systems existed long before the i486;
    nothing the 486 did was new.

    I treaded the indroduction of LOCK XADD just as a hint that sth.
    like this benaphore was thoguht before 1996.

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.
    Think, strong CAS vs weak CAS...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 1 13:01:12 2023
    On 11/1/2023 11:24 AM, Bonita Montero wrote:
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.


    Its a semaphore, not a mutex. Any semaphore can be used for a mutex, hint:

    Binary Semaphore

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Wed Nov 1 20:50:07 2023
    On 2023-11-01, Bonita Montero <[email protected]> wrote:
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published.

    But back then, Ben Affleck had a hotly publicized affair with a hot
    water bottle (thermophore), so that's where that's from.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Wed Nov 1 14:05:02 2023
    On 11/1/2023 1:50 PM, Kaz Kylheku wrote:
    On 2023-11-01, Bonita Montero <[email protected]> wrote:
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published.

    But back then, Ben Affleck had a hotly publicized affair with a hot
    water bottle (thermophore), so that's where that's from.


    LOL! :^D

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Wed Nov 1 14:07:23 2023
    On 11/1/2023 1:50 PM, Kaz Kylheku wrote:
    On 2023-11-01, Bonita Montero <[email protected]> wrote:
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published.

    But back then, Ben Affleck had a hotly publicized affair with a hot
    water bottle (thermophore), so that's where that's from.


    Be sure to check out:

    https://vorbrodt.blog/2019/02/05/fast-semaphore

    ;^)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 2 07:10:17 2023
    Am 01.11.2023 um 21:01 schrieb Chris M. Thomasson:
    On 11/1/2023 11:24 AM, Bonita Montero wrote:
    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.


    Its a semaphore, not a mutex. Any semaphore can be used for a mutex, hint:

    You can say that each binary semaphore is a mutex. And a semaphore
    is usually not a compound of userland and kernel facilities, but a
    pure kernel resource.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Thu Nov 2 14:58:57 2023
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No. Each is distinct.

    It is correct, however, to say that both provide the same functionality.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 2 16:44:09 2023
    Am 02.11.2023 um 15:58 schrieb Scott Lurndal:

    No. Each is distinct.

    You can initialize a binary semaphore with true and use it
    as a mutex. And a mutex can be realized without a short path
    but just with a binary semaphore in the mentioned way.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Scott Lurndal on Thu Nov 2 16:34:28 2023
    On 02/11/2023 15:58, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No. Each is distinct.

    It is correct, however, to say that both provide the same functionality.


    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a mutex currently held by a low priority thread. Such boosting is normally not
    part of the functionality of a binary semaphore. This makes them very distinctly different on such OS's - a mutex is a lock, a semaphore
    (binary or otherwise) is a signalling mechanism.

    (I don't know if Windows makes such a distinction - it's not an OS you
    use when thread or process priority is important. It is certainly
    common, though not universal, for RTOS's. And while I don't know the
    details, I'd be surprised if Linux and other *nix's didn't have such a distinction.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Thu Nov 2 15:37:29 2023
    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    A semaphore can be used as a mutex, but it isn't one.

    Firstly, it doesn't have to be binary to be used as a mutex.

    A semaphore restricted to binary values can still serve as a mutex for
    the reason that a mutex is never unlocked multiple times in a correct
    program. So if we correctly use a semaphore as a mutex, its count should
    never go over 1, and for that reason, we can get away with using
    semaphore that only counts from 0 to 1.

    A semaphore isn't a mutex because it doesn't incorporate the concept of
    lock ownership.

    The idea that a semaphore represents a locked state, which has an
    associated owner, arise purely by the situational convention of how it
    is applied.

    A semaphore couldn't provide error checking, such as when a mutex
    non-owner tries to unlock, or recursive deadlock detection.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 2 16:48:15 2023
    Am 02.11.2023 um 16:37 schrieb Kaz Kylheku:

    Firstly, it doesn't have to be binary to be used as a mutex.

    No, but when being used as a mutex its actual maximum value is one.

    A semaphore isn't a mutex because it doesn't incorporate the concept
    of lock ownership.

    Thats not dependent on actual implementation but how you consider
    the value of the binary semaphore. If it is false it is owned.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 2 16:46:18 2023
    Am 02.11.2023 um 16:34 schrieb David Brown:

    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a mutex currently held by a low priority thread. Such boosting is normally not
    part of the functionality of a binary semaphore.  Thismakes them very distinctly different on such OS's - a mutex is a lock, a semaphore
    (binary or otherwise) is a signalling mechanism.

    Does Linux actually boost thread priorities with mutexes ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Thu Nov 2 16:00:20 2023
    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 16:37 schrieb Kaz Kylheku:

    Firstly, it doesn't have to be binary to be used as a mutex.

    No, but when being used as a mutex its actual maximum value is one.

    A semaphore isn't a mutex because it doesn't incorporate the concept
    of lock ownership.

    Thats not dependent on actual implementation but how you consider
    the value of the binary semaphore. If it is false it is owned.

    If it is false, it can be understood as representing a locked state.

    Locked isn't necessarily owned.

    The semaphore does not record the ID of an owner.

    In a correct program, a binary semaphore can represent a lock, yet there
    can be legitimate situations where a thread which didn't place the lock releases it.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Thu Nov 2 16:02:04 2023
    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 15:58 schrieb Scott Lurndal:

    No. Each is distinct.

    You can initialize a binary semaphore with true and use it
    as a mutex. And a mutex can be realized without a short path
    but just with a binary semaphore in the mentioned way.

    You can use a label, if and goto as a while loop.

    That doesn't mean if and goto /are/ while.

    Speaking of which, I have often mused that sempahores are like the go to statement of synchronization---which is ironic, given their inventor.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 2 17:26:32 2023
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to David Brown on Thu Nov 2 13:12:44 2023
    On 11/2/2023 8:34 AM, David Brown wrote:
    On 02/11/2023 15:58, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No.  Each is distinct.

    It is correct, however, to say that both provide the same functionality.


    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a mutex currently held by a low priority thread.  Such boosting is normally not
    part of the functionality of a binary semaphore.  This makes them very distinctly different on such OS's - a mutex is a lock, a semaphore
    (binary or otherwise) is a signalling mechanism.

    Right. Basically, any thread/process can increment and/or decrement a semaphore. This is different than pure mutex logic. Even if the mutex
    uses a semaphore under the covers...


    (I don't know if Windows makes such a distinction - it's not an OS you
    use when thread or process priority is important.  It is certainly
    common, though not universal, for RTOS's.  And while I don't know the details, I'd be surprised if Linux and other *nix's didn't have such a distinction.)


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Thu Nov 2 13:18:27 2023
    On 11/2/2023 9:00 AM, Kaz Kylheku wrote:
    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 16:37 schrieb Kaz Kylheku:

    Firstly, it doesn't have to be binary to be used as a mutex.

    No, but when being used as a mutex its actual maximum value is one.

    A semaphore isn't a mutex because it doesn't incorporate the concept
    of lock ownership.

    Thats not dependent on actual implementation but how you consider
    the value of the binary semaphore. If it is false it is owned.

    If it is false, it can be understood as representing a locked state.

    Locked isn't necessarily owned.

    The semaphore does not record the ID of an owner.

    In a correct program, a binary semaphore can represent a lock, yet there
    can be legitimate situations where a thread which didn't place the lock releases it.


    True. This makes me think of robust mutexes... ;^)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Chris M. Thomasson on Fri Nov 3 10:13:25 2023
    On 02/11/2023 21:12, Chris M. Thomasson wrote:
    On 11/2/2023 8:34 AM, David Brown wrote:
    On 02/11/2023 15:58, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No.  Each is distinct.

    It is correct, however, to say that both provide the same functionality. >>>

    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a
    mutex currently held by a low priority thread.  Such boosting is
    normally not part of the functionality of a binary semaphore.  This
    makes them very distinctly different on such OS's - a mutex is a lock,
    a semaphore (binary or otherwise) is a signalling mechanism.

    Right. Basically, any thread/process can increment and/or decrement a semaphore. This is different than pure mutex logic. Even if the mutex
    uses a semaphore under the covers...


    That's a very different issue - though of course it is an equally
    important distinction between mutexes and binary semaphores. (And no, a
    mutex cannot be implemented using semaphores if the mutex has protection against priority inversion, and the semaphore does not.)


    (I don't know if Windows makes such a distinction - it's not an OS you
    use when thread or process priority is important.  It is certainly
    common, though not universal, for RTOS's.  And while I don't know the
    details, I'd be surprised if Linux and other *nix's didn't have such a
    distinction.)



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Fri Nov 3 10:15:52 2023
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. Only the owner can unlock the mutex again. This is
    completely different from semaphores, where by design any thread can
    raise or lower the semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 11:55:20 2023
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread. The mentioned benaphore, which is the
    basic skeleton for all mutexes, is a good example for that.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 13:36:39 2023
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it! Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael S@21:1/5 to Kaz Kylheku on Fri Nov 3 16:10:10 2023
    On Thu, 2 Nov 2023 16:02:04 -0000 (UTC)
    Kaz Kylheku <[email protected]> wrote:

    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 15:58 schrieb Scott Lurndal:

    No. Each is distinct.

    You can initialize a binary semaphore with true and use it
    as a mutex. And a mutex can be realized without a short path
    but just with a binary semaphore in the mentioned way.

    You can use a label, if and goto as a while loop.

    That doesn't mean if and goto /are/ while.

    Speaking of which, I have often mused that sempahores are like the go
    to statement of synchronization---which is ironic, given their
    inventor.


    IMHO, the word 'mutex' has no exact meaning agreed by all or even
    majority of Computer Science community.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael S@21:1/5 to Bonita Montero on Fri Nov 3 16:04:28 2023
    On Wed, 1 Nov 2023 19:24:06 +0100
    Bonita Montero <[email protected]> wrote:

    Am 01.11.2023 um 13:00 schrieb Bonita Montero:

    I guess he wouldn't have given the benaphore its name because its
    the simplest form of a mutex and you can find for sure a lot of
    similar implementations of that on the net.

    I found that the name benaphore was given 1996. Maybe this was the
    first time this idea was published. But this idea is that obvious
    that I had it myself that I think there were several people which
    also had this idea before 1996. A hint on how soon this has been
    invented may be the introduction of the LOCK XADD instruction which
    makes this kind of mutex implementable more efficient than with
    LOCK CMPXCHG because this operation never fails.


    Semophores can be efficiently implemented on "old" x86 by means of
    'LOCK ADD mem, reg'. It is possible due to arithmetic flags generated
    by ADD instructiion. As far as semaphores goes 'LOCK XADD' adds little
    or nothing to efficiency over 'LOCK ADD'. XADD is just easier to use as
    an intrinsic from C or similar languages.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Fri Nov 3 14:41:37 2023
    On 03/11/2023 11:55, Bonita Montero wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when
    the lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread. The mentioned benaphore, which is the
    basic skeleton for all mutexes, is a good example for that.


    You can call that a mutex if you like, but you'd be wrong. You could
    call it a banana, and you would do no worse.


    Wikipedia may not be have any particular authority on its definitions,
    but it explains the difference between mutexes and semaphores quite well:

    <https://en.wikipedia.org/wiki/Lock_(computer_science)#Mutexes_vs._semaphores>


    Critical to the point of a mutex is that only the thread that locks it
    can unlock it.


    You can look at the notes for C++ semaphores here, which make the
    distinction clear:

    <https://en.cppreference.com/w/cpp/thread/counting_semaphore>

    Documentation for C++ mutexes is here :

    <https://en.cppreference.com/w/cpp/thread/mutex> <https://en.cppreference.com/w/cpp/thread/mutex/unlock>

    Or you can read the C++ standards if you prefer. (I find the
    cppreference pages much easier to follow.)

    Or you can look up the documentation for Windows mutexes on MS's webpages.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 15:24:31 2023
    Am 03.11.2023 um 15:04 schrieb Michael S:

    Semophores can be efficiently implemented on "old" x86 by means of
    'LOCK ADD mem, reg'. ...

    Semaphores need much more code and this code is usually in the kernel
    since with a semaphore a thread may go to sleep or be awakened.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 15:22:49 2023
    Am 03.11.2023 um 14:41 schrieb David Brown:
    On 03/11/2023 11:55, Bonita Montero wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when
    the lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread. The mentioned benaphore, which is the
    basic skeleton for all mutexes, is a good example for that.


    You can call that a mutex if you like, but you'd be wrong.  You could
    call it a banana, and you would do no worse.
    Wikipedia may not be have any particular authority on its definitions,
    but it explains the difference between mutexes and semaphores quite well: <https://en.wikipedia.org/wiki/Lock_(computer_science)#Mutexes_vs._semaphores>
    Critical to the point of a mutex is that only the thread that locks it
    can unlock it.
    You can look at the notes for C++ semaphores here, which make the
    distinction clear:

    At this point in the thread I was discussion whether a mutex has the
    notion of an ownership and it mutexes vs. semaphores.


    <https://en.cppreference.com/w/cpp/thread/counting_semaphore>

    Documentation for C++ mutexes is here :

    <https://en.cppreference.com/w/cpp/thread/mutex> <https://en.cppreference.com/w/cpp/thread/mutex/unlock>

    Or you can read the C++ standards if you prefer.  (I find the
    cppreference pages much easier to follow.)

    Or you can look up the documentation for Windows mutexes on MS's webpages.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Fri Nov 3 15:32:38 2023
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    Not POSIX mutexes: pthread_mutex_unlock by a non-owner is either
    undefined behavior, or a condition diagnosed by an error return value, depending on the mutex attributes:

    See the table here:

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html

    E.g. for a norma mutex type which is not attributed as robust,
    it is undefined behavior.

    Support for unlocking by a non-owner is possible in a mutex, but that's
    a non-mutex-like behavioral extension.

    A mutex that is owned by a process, rather than a thread, can be locked
    by one thread and unlocked by another. Classic POSIX file locks (F_SETLK
    code of fcntl, etc) are like this, IIRC. That's still mutex-like, just
    the scope is process, rather than thread.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 16:49:56 2023
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive. Otherwise you
    can easily unlock a mutex which was locked in another thread.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Fri Nov 3 16:35:39 2023
    On 03/11/2023 15:22, Bonita Montero wrote:
    Am 03.11.2023 um 14:41 schrieb David Brown:
    On 03/11/2023 11:55, Bonita Montero wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when
    the lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread. The mentioned benaphore, which is the
    basic skeleton for all mutexes, is a good example for that.


    You can call that a mutex if you like, but you'd be wrong.  You could
    call it a banana, and you would do no worse.
    Wikipedia may not be have any particular authority on its definitions,
    but it explains the difference between mutexes and semaphores quite well:
    <https://en.wikipedia.org/wiki/Lock_(computer_science)#Mutexes_vs._semaphores>
    Critical to the point of a mutex is that only the thread that locks it
    can unlock it.
    You can look at the notes for C++ semaphores here, which make the
    distinction clear:

    At this point in the thread I was discussion whether a mutex has the
    notion of an ownership and it mutexes vs. semaphores.


    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. A semaphore is not a lock, it is a
    signalling mechanism - and any thread can signal it up or down. A mutex
    is a locking mechanism to ensure mutually exclusive access to some
    resource, and only the owner can release the lock (baring exceptional circumstances, such as a process being killed by the OS). A sort-of
    mutex where any thread could release the lock would be as useful as a
    chocolate teapot - as would a semaphore which could only be lowered by
    the thread that raised it.


    <https://en.cppreference.com/w/cpp/thread/counting_semaphore>

    Documentation for C++ mutexes is here :

    <https://en.cppreference.com/w/cpp/thread/mutex>
    <https://en.cppreference.com/w/cpp/thread/mutex/unlock>

    Or you can read the C++ standards if you prefer.  (I find the
    cppreference pages much easier to follow.)

    Or you can look up the documentation for Windows mutexes on MS's
    webpages.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Fri Nov 3 18:02:39 2023
    On 03/11/2023 16:49, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive. Otherwise you
    can easily unlock a mutex which was locked in another thread.


    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of
    execution, otherwise, the behavior is undefined."

    Please read the C++ standards, section [thread.mutex.requirements.mutex]
    and note that for "unlock()", there is the sentence "Preconditions: The
    calling thread owns the mutex."

    Please learn what "undefined behaviour" means, and what "preconditions"
    mean.

    Then come back and apologise.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.


    Using a semaphore as a lock is not recommended because a semaphore does
    not make a good lock. A main reason for that is that it has no concept
    of ownership. /All/ locking and inter-thread signalling mechanisms rely
    on kernel facilities, because they need to be able to sleep, and to wake sleeping threads.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Fri Nov 3 16:53:36 2023
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    That has to be the worst idea you've proposed so far.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Kaz Kylheku on Fri Nov 3 18:05:52 2023
    On 03/11/2023 16:32, Kaz Kylheku wrote:
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    Not POSIX mutexes: pthread_mutex_unlock by a non-owner is either
    undefined behavior, or a condition diagnosed by an error return value, depending on the mutex attributes:

    See the table here:

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html

    E.g. for a norma mutex type which is not attributed as robust,
    it is undefined behavior.

    Support for unlocking by a non-owner is possible in a mutex, but that's
    a non-mutex-like behavioral extension.

    A mutex that is owned by a process, rather than a thread, can be locked
    by one thread and unlocked by another. Classic POSIX file locks (F_SETLK
    code of fcntl, etc) are like this, IIRC. That's still mutex-like, just
    the scope is process, rather than thread.


    Mutexes are locks, but not all locks are mutexes. File locks are not
    mutexes - they are for controlling access to files, and it might
    (rarely) make sense to unlock them from a different thread than the
    locking thread.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 18:08:15 2023
    Am 03.11.2023 um 18:02 schrieb David Brown:

    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined."

    Maybe, but I'm talking about how mutexes usually work internally.


    Using a semaphore as a lock is not recommended because a semaphore does
    not make a good lock.  A main reason for that is that it has no concept
    of ownership. ...

    Mutexes also usually have no notion of ownership.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 18:09:39 2023
    Am 03.11.2023 um 17:53 schrieb Scott Lurndal:
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    That has to be the worst idea you've proposed so far.

    I won't say that this is recommendable but just that it is possible
    from the way how a mutex usually works. I just wanted to say with
    that that mutexes usually have no ownership. You can see that with
    the benapohre.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Fri Nov 3 17:28:51 2023
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive. Otherwise you
    can easily unlock a mutex which was locked in another thread.

    That depends entirely on which kind of mutex on what platform.

    - POSIX mutexes make that either an error, or undefined behavior,
    depending on the mutex attributes.

    - MSDN says this about critical sections: "If a thread calls
    LeaveCriticalSection when it does not have ownership of the specified
    critical section object, an error occurs that may cause another thread
    using EnterCriticalSection to wait indefinitely."

    - MSDN, again, about Win32 mutex objects: "The ReleaseMutex function
    fails if the calling thread does not own the mutex object."

    - ISO C (since C11) mtx_unlock: undefined behavior if mutex is not
    locked by the calling thread.

    - C++ std::mutex::unlock: ditto.

    At this point, I'm already well into stuff I don't give a shit about,
    to look for more. I don't care if ScrotelyOS developed in Ruritania
    between 1973 and 1981 had "mutexes" that had up to three owners.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Fri Nov 3 17:29:40 2023
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 18:02 schrieb David Brown:

    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of
    execution, otherwise, the behavior is undefined."

    Maybe, but I'm talking about how mutexes usually work internally.

    "Never mind that a[i++] = i is undefined; I'm talking about how it
    works internally, damn it!!!"

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 18:30:55 2023
    Am 03.11.2023 um 18:28 schrieb Kaz Kylheku:

    That depends entirely on which kind of mutex on what platform.

    - POSIX mutexes make that either an error, or undefined behavior,
    depending on the mutex attributes.

    - MSDN says this about critical sections: "If a thread calls
    LeaveCriticalSection when it does not have ownership of the specified
    critical section object, an error occurs that may cause another thread
    using EnterCriticalSection to wait indefinitely."

    - MSDN, again, about Win32 mutex objects: "The ReleaseMutex function
    fails if the calling thread does not own the mutex object."

    - ISO C (since C11) mtx_unlock: undefined behavior if mutex is not
    locked by the calling thread.

    - C++ std::mutex::unlock: ditto.

    At this point, I'm already well into stuff I don't give a shit about,
    to look for more. I don't care if ScrotelyOS developed in Ruritania
    between 1973 and 1981 had "mutexes" that had up to three owners.

    The discussed benaphore allows this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Fri Nov 3 18:03:48 2023
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 18:28 schrieb Kaz Kylheku:

    That depends entirely on which kind of mutex on what platform.

    - POSIX mutexes make that either an error, or undefined behavior,
    depending on the mutex attributes.

    - MSDN says this about critical sections: "If a thread calls
    LeaveCriticalSection when it does not have ownership of the specified
    critical section object, an error occurs that may cause another thread
    using EnterCriticalSection to wait indefinitely."

    - MSDN, again, about Win32 mutex objects: "The ReleaseMutex function
    fails if the calling thread does not own the mutex object."

    - ISO C (since C11) mtx_unlock: undefined behavior if mutex is not
    locked by the calling thread.

    - C++ std::mutex::unlock: ditto.

    At this point, I'm already well into stuff I don't give a shit about,
    to look for more. I don't care if ScrotelyOS developed in Ruritania
    between 1973 and 1981 had "mutexes" that had up to three owners.

    The discussed benaphore allows this.

    That's a kind of semaphore, and its name reflects that.

    Benoit Schillings introduced this word as an optimization that is
    for certain inefficient semaphore implementations:

    https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html#Engineering1-26

    He recommends this:

    void acquire_benaphore()
    {
    long previous_value;
    previous_value = atomic_add (&benaphore_atom, 1);

    if (previous_value > 0)
    acquire_sem(benaphore_sem);
    }

    Firstly, this is not a mutex but a semaphore. Secondly, it only makes
    sense if acquire_sem is somehow expensive (like a trap to the kernel).

    The reason is that acquire_sem already looks like acquire_benaphore:

    void acquire_sem(semaphore *s)
    {
    long prev = atomic_add(&s->counter);

    if (prev > 0) {
    // suspend caller somehow
    }

    // ...

    }


    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 19:07:18 2023
    Am 03.11.2023 um 19:03 schrieb Kaz Kylheku:
    On 2023-11-03, Bonita Montero <[email protected]> wrote:

    Am 03.11.2023 um 18:28 schrieb Kaz Kylheku:

    The discussed benaphore allows this.

    That's a kind of semaphore, and its name reflects that.

    It's a mutex, look at the code.
    And look at the discussion on stack overflow about that: https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 19:08:51 2023
    Am 03.11.2023 um 19:07 schrieb Bonita Montero:

    It's a mutex, look at the code.
    And look at the discussion on stack overflow about that: https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss

    And this article also refers to it as a mutex: https://preshing.com/20120226/roll-your-own-lightweight-mutex/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Fri Nov 3 18:52:47 2023
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 18:02 schrieb David Brown:

    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of
    execution, otherwise, the behavior is undefined."

    Maybe, but I'm talking about how mutexes usually work internally.

    Why does that matter? It's the behavior seen by the code
    using the mutex that matters.

    Undefined behavior isn't something an application can rely on.

    You keep digging yourself deeper and deeper.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to David Brown on Fri Nov 3 18:51:47 2023
    David Brown <[email protected]> writes:
    On 03/11/2023 16:49, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive. Otherwise you
    can easily unlock a mutex which was locked in another thread.


    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of >execution, otherwise, the behavior is undefined."

    Please read the C++ standards, section [thread.mutex.requirements.mutex]
    and note that for "unlock()", there is the sentence "Preconditions: The >calling thread owns the mutex."


    And read the POSIX standard as well.

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html

    Please learn what "undefined behaviour" means, and what "preconditions"
    mean.

    Then come back and apologise.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.


    Using a semaphore as a lock is not recommended because a semaphore does
    not make a good lock. A main reason for that is that it has no concept
    of ownership. /All/ locking and inter-thread signalling mechanisms rely
    on kernel facilities, because they need to be able to sleep, and to wake >sleeping threads.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Fri Nov 3 18:54:23 2023
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 18:28 schrieb Kaz Kylheku:

    That depends entirely on which kind of mutex on what platform.

    - POSIX mutexes make that either an error, or undefined behavior,
    depending on the mutex attributes.

    - MSDN says this about critical sections: "If a thread calls
    LeaveCriticalSection when it does not have ownership of the specified
    critical section object, an error occurs that may cause another thread
    using EnterCriticalSection to wait indefinitely."

    - MSDN, again, about Win32 mutex objects: "The ReleaseMutex function
    fails if the calling thread does not own the mutex object."

    - ISO C (since C11) mtx_unlock: undefined behavior if mutex is not
    locked by the calling thread.

    - C++ std::mutex::unlock: ditto.

    At this point, I'm already well into stuff I don't give a shit about,
    to look for more. I don't care if ScrotelyOS developed in Ruritania
    between 1973 and 1981 had "mutexes" that had up to three owners.

    The discussed benaphore allows this.

    And which operating system offers this soi disant 'benaphore' as a synchronization mechanism?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Fri Nov 3 20:25:04 2023
    Am 03.11.2023 um 19:54 schrieb Scott Lurndal:

    And which operating system offers this soi disant 'benaphore' as a synchronization mechanism?

    Non-recursive mutexes mostly base on the same mechanisms since
    an atomic add doesn't need looping.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Fri Nov 3 20:56:15 2023
    On 2023-11-03, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 19:54 schrieb Scott Lurndal:

    And which operating system offers this soi disant 'benaphore' as a
    synchronization mechanism?

    Non-recursive mutexes mostly base on the same mechanisms since
    an atomic add doesn't need looping.

    An atomic counter, featuring a wait when the value is in a certain
    range, is known as semaphore.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Branimir Maksimovic@21:1/5 to Bonita Montero on Fri Nov 3 21:18:59 2023
    On 2023-10-30, Bonita Montero <[email protected]> wrote:
    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:

    #if defined(_WIN32)
    #include <Windows.h>
    #elif defined(__unix__)
    #include <sys/resource.h>
    #endif
    #include <iostream>
    #include <thread>
    #include <semaphore>
    #include <atomic>
    #include <utility>
    #include <vector>

    using namespace std;

    int main()
    {
    binary_semaphore
    semA( 1 ),
    semB( 0 );
    auto getTimes = []() -> pair<int64_t, int64_t>
    {
    #if defined(_WIN32)
    FILETIME ftDummy, ftKernel, ftUser;
    GetThreadTimes( GetCurrentThread(), &ftDummy, &ftDummy, &ftKernel,
    &ftUser );
    auto compose = []( FILETIME &ft ) { return ((uint64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime) * 100; };
    return { compose( ftKernel ), compose( ftUser ) };
    #elif defined(__unix__)
    rusage ru;
    getrusage( RUSAGE_THREAD, &ru );
    auto compose = []( timeval &tv ) -> int64_t { return (uint64_t)tv.tv_sec * 1'000'000'000u + (uint32_t)tv.tv_usec * 1'000u; };
    return { compose( ru.ru_stime ), compose( ru.ru_utime ) }; #endif
    };
    atomic_int64_t
    aFlips( 0 ),
    aKernel( 0 ),
    aUser( 0 );
    atomic_bool stop( false );
    auto thr = [&]( binary_semaphore &semMe, binary_semaphore &semYou )
    {
    auto tBegin = getTimes();
    uint64_t f = 0;
    for( ; !stop.load( memory_order_relaxed ); ++f )
    semMe.acquire(),
    semYou.release();
    auto tEnd = getTimes();
    aFlips += f;
    aKernel += tEnd.first - tBegin.first;
    aUser += tEnd.second + tBegin.second;
    };
    vector<jthread> threads;
    threads.emplace_back( thr, ref( semA ), ref( semB ) );
    threads.emplace_back( thr, ref( semB ), ref( semA ) );
    this_thread::sleep_for( 1s );
    stop = true;
    threads.resize( 0 );
    double
    kernel = (double)aKernel,
    user = (double)aUser,
    total = kernel + user,
    flips = (double)aFlips;
    cout << user / total * 100.0 << "%" << endl;
    cout << total / flips << "ns" << endl;
    }

    Results unter Windows 11 on a AMD Ryzen 7950X (Zen4):
    100%
    89.3523ns
    So 100% of the CPU time is spent in user space and a flip back
    and forth takes 90ns.
    Results unter Ubuntu 20.04 on a AMD Threadripper 3990X (Zen2):
    6.56209%
    476.213ns
    So under Linux most of the time is spent in kernel space and I think
    about 0.5us, thats about 2000 clock cycles with full boost, isn't bad
    for an explicit kernel call.
    I think this userland spinning isn't really necessary because you
    normally a usual mutex and a condition_variable fits for everything
    you need. An explicit semaphore is rather needed if you build your
    own synchronization primitves on top of the semaphore. And if you do
    that you usually have your own spinning with that and you don't need
    further spinning if you want go into kernel mode. But this additional spinning actually doesn't really hurt since the kernel call itself
    costs a lot more.

    maxa@Branimirs-MacBook-Air News % g++ -O2 run.cpp -o run -std=c++20 run.cpp:37:2: warning: non-void lambda does not return a value [-Wreturn-type]
    };
    ^
    1 warning generated.
    bmaxa@Branimirs-MacBook-Air News % ./run
    libc++abi: terminating
    zsh: abort ./run

    --

    7-77-777, Evil Sinner! https://www.linkedin.com/in/branimir-maksimovic-6762bbaa/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:13:47 2023
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:14:56 2023
    On 11/3/2023 7:24 AM, Bonita Montero wrote:
    Am 03.11.2023 um 15:04 schrieb Michael S:

    Semophores can be efficiently implemented on "old" x86 by means of
    'LOCK ADD mem, reg'. ...

    Semaphores need much more code and this code is usually in the kernel
    since with a semaphore a thread may go to sleep or be awakened.

    Huh? A semaphore in userspace is doable. We use a kernel semaphore for
    the slow paths.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:18:22 2023
    On 11/3/2023 3:55 AM, Bonita Montero wrote:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when
    the lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    Oh, really!?!?!? Yikes!

    The mentioned benaphore, which is the
    basic skeleton for all mutexes, is a good example for that.

    Oh my. A benaphore is basically a userspace semaphore using a kernel
    semaphore for slow paths.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to David Brown on Fri Nov 3 15:16:17 2023
    On 11/3/2023 2:13 AM, David Brown wrote:
    On 02/11/2023 21:12, Chris M. Thomasson wrote:
    On 11/2/2023 8:34 AM, David Brown wrote:
    On 02/11/2023 15:58, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No.  Each is distinct.

    It is correct, however, to say that both provide the same
    functionality.


    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a
    mutex currently held by a low priority thread.  Such boosting is
    normally not part of the functionality of a binary semaphore.  This
    makes them very distinctly different on such OS's - a mutex is a
    lock, a semaphore (binary or otherwise) is a signalling mechanism.

    Right. Basically, any thread/process can increment and/or decrement a
    semaphore. This is different than pure mutex logic. Even if the mutex
    uses a semaphore under the covers...


    That's a very different issue - though of course it is an equally
    important distinction between mutexes and binary semaphores.  (And no, a mutex cannot be implemented using semaphores if the mutex has protection against priority inversion, and the semaphore does not.)

    Agreed. Basically, the benaphore is a bakery algorithm. It has FIFO on
    the mind... So, you are right.




    (I don't know if Windows makes such a distinction - it's not an OS
    you use when thread or process priority is important.  It is
    certainly common, though not universal, for RTOS's.  And while I
    don't know the details, I'd be surprised if Linux and other *nix's
    didn't have such a distinction.)




    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:19:04 2023
    On 11/3/2023 8:49 AM, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive.

    WHAT!!! ? ;^o


    Otherwise you
    can easily unlock a mutex which was locked in another thread.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to David Brown on Fri Nov 3 15:20:48 2023
    On 11/3/2023 10:02 AM, David Brown wrote:
    On 03/11/2023 16:49, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive. Otherwise you
    can easily unlock a mutex which was locked in another thread.


    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined."

    Please read the C++ standards, section [thread.mutex.requirements.mutex]
    and note that for "unlock()", there is the sentence "Preconditions: The calling thread owns the mutex."

    Please learn what "undefined behaviour" means, and what "preconditions"
    mean.

    Then come back and apologise.

    If I wait for that day, well, I will most likely be dead.



    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.


    Using a semaphore as a lock is not recommended because a semaphore does
    not make a good lock.  A main reason for that is that it has no concept
    of ownership.  /All/ locking and inter-thread signalling mechanisms rely
    on kernel facilities, because they need to be able to sleep, and to wake sleeping threads.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:21:19 2023
    On 11/3/2023 10:08 AM, Bonita Montero wrote:
    Am 03.11.2023 um 18:02 schrieb David Brown:

    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of
    execution, otherwise, the behavior is undefined."

    Maybe, but I'm talking about how mutexes usually work internally.


    Using a semaphore as a lock is not recommended because a semaphore
    does not make a good lock.  A main reason for that is that it has no
    concept of ownership. ...

    Mutexes also usually have no notion of ownership.


    The Art of Trolling, 123, all is me...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:25:10 2023
    On 11/3/2023 10:09 AM, Bonita Montero wrote:
    Am 03.11.2023 um 17:53 schrieb Scott Lurndal:
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the >>>> lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    That has to be the worst idea you've proposed so far.

    I won't say that this is recommendable but just that it is possible
    from the way how a mutex usually works. I just wanted to say with
    that that mutexes usually have no ownership. You can see that with
    the benapohre.


    Oh shit. You have dug yourself into a really deep hole here, Bonita.

    wow.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Scott Lurndal on Fri Nov 3 15:24:07 2023
    On 11/3/2023 9:53 AM, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 10:15 schrieb David Brown:
    On 02/11/2023 17:26, Bonita Montero wrote:
    Am 02.11.2023 um 17:00 schrieb Kaz Kylheku:

    Locked isn't necessarily owned.
    The semaphore does not record the ID of an owner.

    A non-recursive mutes also doesn't do this. The surrounding
    code itself is designed in a way that it behaves like an owner.

    Mutexes - recursive or non-recursive - /do/ record their owner when the
    lock is taken. ...

    Absolutely not. You can take ownership of a mutex in one thread and
    release it in another thread.

    That has to be the worst idea you've proposed so far.

    That should be framed! ;^O

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Fri Nov 3 15:26:53 2023
    On 11/3/2023 11:08 AM, Bonita Montero wrote:
    Am 03.11.2023 um 19:07 schrieb Bonita Montero:

    It's a mutex, look at the code.
    And look at the discussion on stack overflow about that:
    https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss

    And this article also refers to it as a mutex: https://preshing.com/20120226/roll-your-own-lightweight-mutex/


    Oh my god...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sat Nov 4 00:01:12 2023
    On 11/1/2023 12:59 PM, Chris M. Thomasson wrote:
    On 11/1/2023 5:21 AM, Bonita Montero wrote:
    Am 31.10.2023 um 21:59 schrieb Chris M. Thomasson:

    Why do you say that?

    The article you mentioned is also discussed here:
    https://stackoverflow.com/questions/1635416/are-benaphores-worth-implementing-on-modern-oss
    The first replier says the same I said: its a normal mutex.


    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    Your theme song?

    https://youtu.be/FE-hBbBBGBs

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sat Nov 4 00:19:12 2023
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    CMPXCHG cannot fail spuriously. However, LL/SC can.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:13:53 2023
    Am 04.11.2023 um 08:19 schrieb Chris M. Thomasson:
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:

    On 11/3/2023 5:36 AM, Bonita Montero wrote:

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.

    CMPXCHG cannot fail spuriously. However, LL/SC can.

    Youre telling things I've told above in a different way.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:15:05 2023
    Am 03.11.2023 um 21:56 schrieb Kaz Kylheku:

    An atomic counter, featuring a wait when the value is in a certain
    range, is known as semaphore.

    An atomic counter alone isn't sufficient for a semaphore because
    there's no way to sleep or to be awakened with that.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:16:16 2023
    Am 03.11.2023 um 23:19 schrieb Chris M. Thomasson:
    On 11/3/2023 8:49 AM, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive.

    WHAT!!! ? ;^o

    It't the surrounding code that "knows" that it is
    an owner, not the mutex itself. A non-recurive mutex
    just knows that it is owned, but not by whom.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:17:22 2023
    Am 03.11.2023 um 23:25 schrieb Chris M. Thomasson:

    On 11/3/2023 10:09 AM, Bonita Montero wrote:

    I won't say that this is recommendable but just that it is possible
    from the way how a mutex usually works. I just wanted to say with
    that that mutexes usually have no ownership. You can see that with
    the benapohre.

    Oh shit. You have dug yourself into a really deep hole here, Bonita.

    The benaphore does know that it is owned, but not by whom.
    Its the surrounding code that knows that it owns the mutex.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:18:21 2023
    Am 03.11.2023 um 23:18 schrieb Chris M. Thomasson:

    Oh my. A benaphore is basically a userspace semaphore using a kernel semaphore for slow paths.

    There isn't sth. like a userspace semaphore since with a sempahore
    you need support to sleep or to be awakened.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:19:46 2023
    Am 01.11.2023 um 20:59 schrieb Chris M. Thomasson:

    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    Google for "benaphore mutex" and you'll find several refernces that a
    benaphore is a mutex, including the two articles I've referred so far.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:25:26 2023
    Am 03.11.2023 um 23:19 schrieb Chris M. Thomasson:
    On 11/3/2023 8:49 AM, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including the
    C++ standard library, the Windows API, and AFAIK POSIX), mutexes have
    ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive.

    WHAT!!! ? ;^o

    It's not necessary to record ownership of a specific thread for a non
    recursive mutex but just _that_ a mutex is ownend, and not by whom.
    The benaphore can easily locked in one thread and unlocked in another
    thread, although this usually doesn't happen.
    It's just the surrounding code that successfully locked a mutex that
    "knows" it is the owner.

    Otherwise you
    can easily unlock a mutex which was locked in another thread.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.




    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:29:18 2023
    Maybe I missed some header which is implicitly included with me.
    The code compiles fine with g++, clang++, clang-cl and MSVC on
    my PC.

    Am 03.11.2023 um 22:18 schrieb Branimir Maksimovic:
    On 2023-10-30, Bonita Montero <[email protected]> wrote:
    MSVC's C++20 semaphore class spins to acquire the semaphore;
    g++'s / libstdc++'s doesn't spin. I wote a little test program
    to rest how long it takes to flip to another thread and back
    with a binary_semaphore (always a counting_semaphore<1>).
    Here it is:

    #if defined(_WIN32)
    #include <Windows.h>
    #elif defined(__unix__)
    #include <sys/resource.h>
    #endif
    #include <iostream>
    #include <thread>
    #include <semaphore>
    #include <atomic>
    #include <utility>
    #include <vector>

    using namespace std;

    int main()
    {
    binary_semaphore
    semA( 1 ),
    semB( 0 );
    auto getTimes = []() -> pair<int64_t, int64_t>
    {
    #if defined(_WIN32)
    FILETIME ftDummy, ftKernel, ftUser;
    GetThreadTimes( GetCurrentThread(), &ftDummy, &ftDummy, &ftKernel,
    &ftUser );
    auto compose = []( FILETIME &ft ) { return
    ((uint64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime) * 100; };
    return { compose( ftKernel ), compose( ftUser ) };
    #elif defined(__unix__)
    rusage ru;
    getrusage( RUSAGE_THREAD, &ru );
    auto compose = []( timeval &tv ) -> int64_t { return
    (uint64_t)tv.tv_sec * 1'000'000'000u + (uint32_t)tv.tv_usec * 1'000u; };
    return { compose( ru.ru_stime ), compose( ru.ru_utime ) };
    #endif
    };
    atomic_int64_t
    aFlips( 0 ),
    aKernel( 0 ),
    aUser( 0 );
    atomic_bool stop( false );
    auto thr = [&]( binary_semaphore &semMe, binary_semaphore &semYou )
    {
    auto tBegin = getTimes();
    uint64_t f = 0;
    for( ; !stop.load( memory_order_relaxed ); ++f )
    semMe.acquire(),
    semYou.release();
    auto tEnd = getTimes();
    aFlips += f;
    aKernel += tEnd.first - tBegin.first;
    aUser += tEnd.second + tBegin.second;
    };
    vector<jthread> threads;
    threads.emplace_back( thr, ref( semA ), ref( semB ) );
    threads.emplace_back( thr, ref( semB ), ref( semA ) );
    this_thread::sleep_for( 1s );
    stop = true;
    threads.resize( 0 );
    double
    kernel = (double)aKernel,
    user = (double)aUser,
    total = kernel + user,
    flips = (double)aFlips;
    cout << user / total * 100.0 << "%" << endl;
    cout << total / flips << "ns" << endl;
    }

    Results unter Windows 11 on a AMD Ryzen 7950X (Zen4):
    100%
    89.3523ns
    So 100% of the CPU time is spent in user space and a flip back
    and forth takes 90ns.
    Results unter Ubuntu 20.04 on a AMD Threadripper 3990X (Zen2):
    6.56209%
    476.213ns
    So under Linux most of the time is spent in kernel space and I think
    about 0.5us, thats about 2000 clock cycles with full boost, isn't bad
    for an explicit kernel call.
    I think this userland spinning isn't really necessary because you
    normally a usual mutex and a condition_variable fits for everything
    you need. An explicit semaphore is rather needed if you build your
    own synchronization primitves on top of the semaphore. And if you do
    that you usually have your own spinning with that and you don't need
    further spinning if you want go into kernel mode. But this additional
    spinning actually doesn't really hurt since the kernel call itself
    costs a lot more.

    maxa@Branimirs-MacBook-Air News % g++ -O2 run.cpp -o run -std=c++20 run.cpp:37:2: warning: non-void lambda does not return a value [-Wreturn-type]
    };
    ^
    1 warning generated.
    bmaxa@Branimirs-MacBook-Air News % ./run
    libc++abi: terminating
    zsh: abort ./run


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 10:53:54 2023
    Am 04.11.2023 um 10:19 schrieb Bonita Montero:

    Google for "benaphore mutex" ...
    Or rather "benaphore mutex -semaphore".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 16:58:22 2023
    Am 04.11.2023 um 16:53 schrieb Scott Lurndal:
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it! >>>>> Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    CMPXCHG cannot fail spuriously. However, LL/SC can.

    LL/SC is not compare and swap.

    Cite where I said sth. opposite.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Chris M. Thomasson on Sat Nov 4 15:53:35 2023
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    CMPXCHG cannot fail spuriously. However, LL/SC can.

    LL/SC is not compare and swap.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Chris M. Thomasson on Sat Nov 4 16:03:27 2023
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    BTW - Compare and Swap was invented by an engineer at IBM -
    CAS is a backronym, the inventors initials are CAS.

    https://www.garlic.com/~lynn/2004l.html#56


    "charlie had come up with compare and swap at the science scenter
    http://www.garlic.com/~lynn/subtopic.html#545tech
    based on a lot of work he was doing in fine grain locking (late 60s)
    ... and tried to get it into 370 architecture. POK architecture owners
    came back and said it wasn't possible to justify a multiprocessor-specific
    instruction for the 370 architecture (already having test&set) ... and that
    to get it justified, it would be necessary to come up with a non-multiprocessor
    use for the instruction. thus was born the description for multi-threaded
    application use in non-locked regions .... when running on either
    multiprocessor or non-multiprocessor machines. this was originally
    included in the 370 prinicple of operations as programming notes
    associated with the compare&swap instruction(s). the description has
    since been expanded and moved to the principles of operation appendix.

    note that the choice of compare and swap comes from needing a mnemonic
    that matched charlie's initials (CAS). the mnemonic was slightly changed
    for inclusion in 370 to CS (compare and swap) and CDS (compare double and swap).

    the instructions have since been expanded for 64-bit operation and a
    new "perform locked operation" has since been added.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Sat Nov 4 16:05:31 2023
    Bonita Montero <[email protected]> writes:
    Am 03.11.2023 um 21:56 schrieb Kaz Kylheku:

    An atomic counter, featuring a wait when the value is in a certain
    range, is known as semaphore.

    An atomic counter alone isn't sufficient for a semaphore because
    there's no way to sleep or to be awakened with that.

    Sure there is. It's even obvious.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 17:35:13 2023
    Am 04.11.2023 um 17:22 schrieb David Brown:

    I think, from this thread, it is clear that you have no idea how mutexes
    are used. ...

    I use them every day and I've developed my own reader's-writer-lock.

    I stand corrected.  Your idea of "usually" is limited to no OS in
    existence.

    A non-recurive mutex doensn't need to know which thread is the
    owner, but just that there's an owner.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Sat Nov 4 17:22:35 2023
    On 03/11/2023 18:08, Bonita Montero wrote:
    Am 03.11.2023 um 18:02 schrieb David Brown:

    Please read <https://en.cppreference.com/w/cpp/thread/mutex/unlock>.
    Note the sentence "The mutex must be locked by the current thread of
    execution, otherwise, the behavior is undefined."

    Maybe, but I'm talking about how mutexes usually work internally.


    I think, from this thread, it is clear that you have no idea how mutexes
    are used. I therefore assume you have no idea how they work internally.
    And I am entirely confident that your idea of "usually" is limited to
    one single OS.


    Using a semaphore as a lock is not recommended because a semaphore
    does not make a good lock.  A main reason for that is that it has no
    concept of ownership. ...

    Mutexes also usually have no notion of ownership.


    I stand corrected. Your idea of "usually" is limited to no OS in existence.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Sat Nov 4 17:56:00 2023
    On 04/11/2023 17:35, Bonita Montero wrote:
    Am 04.11.2023 um 17:22 schrieb David Brown:

    I think, from this thread, it is clear that you have no idea how
    mutexes are used. ...

    I use them every day and I've developed my own reader's-writer-lock.


    Do you do all your work relying on luck?

    I stand corrected.  Your idea of "usually" is limited to no OS in
    existence.

    A non-recurive mutex doensn't need to know which thread is the
    owner, but just that there's an owner.


    No, it needs to know the owner - because only the owner is allowed to
    release the lock.

    I suppose you could make a half-arsed mutex implementation that relies
    on programmers never making mistakes, and just let the nasal demons fly
    when people like you can't read and follow specifications.

    And even if you have such a poor mutex implementation, conceptually
    there is always an owner when the lock is taken - that's the point of
    the thing.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 18:18:45 2023
    Am 04.11.2023 um 17:56 schrieb David Brown:


    No, it needs to know the owner - because only the owner is allowed to
    release the lock.

    This is a superfluous check that does not determine function.

    I suppose you could make a half-arsed mutex implementation that relies
    on programmers never making mistakes, and just let the nasal demons fly
    when people like you can't read and follow specifications.

    It may be possible to release a mutex from another thread, but as no
    one does this this check doesn't make sense. That that much unlikely
    that that happens that it doesn't make sense as a debugging aid, even
    more in C++, where you can't forget unlocking inside the same thread
    if you use a unique_lock<> or lock_guard<>.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Sat Nov 4 18:44:23 2023
    On 2023-11-04, Bonita Montero <[email protected]> wrote:
    Am 03.11.2023 um 21:56 schrieb Kaz Kylheku:

    An atomic counter, featuring a wait when the value is in a certain
    range, is known as semaphore.

    An atomic counter alone isn't sufficient for a semaphore because
    there's no way to sleep or to be awakened with that.

    You're not successfully making it appear as if I said that.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Scott Lurndal on Sat Nov 4 11:44:02 2023
    On 11/4/2023 8:53 AM, Scott Lurndal wrote:
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it! >>>>> Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    CMPXCHG cannot fail spuriously. However, LL/SC can.

    LL/SC is not compare and swap.



    LL/SC can be used to implement a weak CAS. Also, wrt the algorithm, it
    can be used to implement a strong CAS.

    CMPXCHG over on intel cannot be used to implement a weak CAS.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Scott Lurndal on Sat Nov 4 11:45:31 2023
    On 11/4/2023 9:03 AM, Scott Lurndal wrote:
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it!
    Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    BTW - Compare and Swap was invented by an engineer at IBM -
    CAS is a backronym, the inventors initials are CAS.

    https://www.garlic.com/~lynn/2004l.html#56


    "charlie had come up with compare and swap at the science scenter
    http://www.garlic.com/~lynn/subtopic.html#545tech
    based on a lot of work he was doing in fine grain locking (late 60s)
    ... and tried to get it into 370 architecture. POK architecture owners
    came back and said it wasn't possible to justify a multiprocessor-specific
    instruction for the 370 architecture (already having test&set) ... and that
    to get it justified, it would be necessary to come up with a non-multiprocessor
    use for the instruction. thus was born the description for multi-threaded
    application use in non-locked regions .... when running on either
    multiprocessor or non-multiprocessor machines. this was originally
    included in the 370 prinicple of operations as programming notes
    associated with the compare&swap instruction(s). the description has
    since been expanded and moved to the principles of operation appendix.

    note that the choice of compare and swap comes from needing a mnemonic
    that matched charlie's initials (CAS). the mnemonic was slightly changed
    for inclusion in 370 to CS (compare and swap) and CDS (compare double and swap).

    the instructions have since been expanded for 64-bit operation and a
    new "perform locked operation" has since been added.



    Yup. I have actually conversed with Lynn over on comp.arch. Very smart
    person loaded up with knowledge.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sat Nov 4 11:48:16 2023
    On 11/4/2023 11:45 AM, Chris M. Thomasson wrote:
    On 11/4/2023 9:03 AM, Scott Lurndal wrote:
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it! >>>>> Although, wrt intel, there is a way to use CMPXCHG without looping.

    I don't believe that, give me some code-example _here_. The expected
    value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with
    LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might
    have been evicted in the meantime. compare_exchange_strong checks for
    this case and loops internally, whereas compare_exchange_weak gives a
    false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    BTW - Compare and Swap was invented by an engineer at IBM -
    CAS is a backronym, the inventors initials are CAS.

    https://www.garlic.com/~lynn/2004l.html#56


       "charlie had come up with compare and swap at the science scenter
          http://www.garlic.com/~lynn/subtopic.html#545tech
        based on a lot of work he was doing in fine grain locking (late 60s) >>     ... and tried to get it into 370 architecture. POK architecture
    owners
        came back and said it wasn't possible to justify a
    multiprocessor-specific
        instruction for the 370 architecture (already having test&set) ...
    and that
        to get it justified, it would be necessary to come up with a
    non-multiprocessor
        use for the instruction. thus was born the description for
    multi-threaded
        application use in non-locked regions .... when running on either
        multiprocessor or non-multiprocessor machines. this was originally
        included in the 370 prinicple of operations as programming notes
        associated with the compare&swap instruction(s). the description has >>     since been expanded and moved to the principles of operation
    appendix.

       note that the choice of compare and swap comes from needing a mnemonic >>    that matched charlie's initials (CAS). the mnemonic was slightly
    changed
       for inclusion in 370 to CS (compare and swap) and CDS (compare
    double and swap).

       the instructions have since been expanded for 64-bit operation and a
       new "perform locked operation" has since been added.



    Yup. I have actually conversed with Lynn over on comp.arch. Very smart
    person loaded up with knowledge.

    The free pool manipulation in IBM's principle of operations, iirc, its
    in an appendix.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sat Nov 4 19:58:06 2023
    Am 04.11.2023 um 17:03 schrieb Scott Lurndal:
    BTW - Compare and Swap was invented by an engineer at IBM -
    ...

    The idea is not that complex that it would have been invented by
    a lot of technichians later. I think that LL/SC is more sexy since
    it allows lock-free stacks with one word.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sat Nov 4 12:14:50 2023
    On 11/4/2023 11:58 AM, Bonita Montero wrote:
    Am 04.11.2023 um 17:03 schrieb Scott Lurndal:
    BTW - Compare and Swap was invented by an engineer at IBM -
    ...

    The idea is not that complex that it would have been invented by
    a lot of technichians later. I think that LL/SC is more sexy since
    it allows lock-free stacks with one word.


    Just be careful wrt the reservation granularity. LL/SC can be prone to livelock.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sat Nov 4 12:44:07 2023
    On 11/4/2023 2:17 AM, Bonita Montero wrote:
    Am 03.11.2023 um 23:25 schrieb Chris M. Thomasson:

    On 11/3/2023 10:09 AM, Bonita Montero wrote:

    I won't say that this is recommendable but just that it is possible
    from the way how a mutex usually works. I just wanted to say with
    that that mutexes usually have no ownership. You can see that with
    the benapohre.

    Oh shit. You have dug yourself into a really deep hole here, Bonita.

    The benaphore does know that it is owned, but not by whom.
    Its the surrounding code that knows that it owns the mutex.

    Oh god. The benaphore is a semaphore. Mutex logic using a semaphore for
    a slow path is different than a semaphore. God damn, man... You are
    confused and/or trolling? Humm...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sat Nov 4 12:51:23 2023
    On 11/4/2023 2:19 AM, Bonita Montero wrote:
    Am 01.11.2023 um 20:59 schrieb Chris M. Thomasson:

    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    Google for "benaphore mutex" and you'll find several refernces that a benaphore is a mutex, including the two articles I've referred so far.


    MORON!!!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sat Nov 4 12:45:28 2023
    On 11/4/2023 2:18 AM, Bonita Montero wrote:
    Am 03.11.2023 um 23:18 schrieb Chris M. Thomasson:

    Oh my. A benaphore is basically a userspace semaphore using a kernel
    semaphore for slow paths.

    There isn't sth. like a userspace semaphore since with a sempahore
    you need support to sleep or to be awakened.



    Huh? The point of the benaphore (a semaphore) is to try to avoid going
    into the kernel to sleep or to be awakened.

    God damn it Bonita!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sat Nov 4 12:42:51 2023
    On 11/4/2023 2:25 AM, Bonita Montero wrote:
    Am 03.11.2023 um 23:19 schrieb Chris M. Thomasson:
    On 11/3/2023 8:49 AM, Bonita Montero wrote:
    Am 03.11.2023 um 16:35 schrieb David Brown:

    And using the definitions held by most people involved (including
    the C++ standard library, the Windows API, and AFAIK POSIX), mutexes
    have ownership and semaphores do not. ...

    Mutexes only have ownership if they're recursive.

    WHAT!!! ? ;^o

    It's not necessary to record ownership of a specific thread for a non recursive mutex but just _that_ a mutex is ownend, and not by whom.
    The benaphore can easily locked in one thread and unlocked in another
    thread, although this usually doesn't happen.
    It's just the surrounding code that successfully locked a mutex that
    "knows" it is the owner.

    The surrounding code is the mutex. It can use a kernel semaphore for
    slow paths. You seem to be radically confused.



    Otherwise you
    can easily unlock a mutex which was locked in another thread.

    A semaphore is not a lock, ...

    It can be used as a lock although it is not recommended because
    a semaphore is usually a pure kernel facility.





    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sat Nov 4 12:53:23 2023
    On 11/4/2023 12:51 PM, Chris M. Thomasson wrote:
    On 11/4/2023 2:19 AM, Bonita Montero wrote:
    Am 01.11.2023 um 20:59 schrieb Chris M. Thomasson:

    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    Google for "benaphore mutex" and you'll find several refernces that a
    benaphore is a mutex, including the two articles I've referred so far.


    MORON!!!

    The benaphore is a semaphore using a bakery algorithm. You are
    completely confused and incoherently flailing around.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sat Nov 4 12:54:51 2023
    On 11/4/2023 12:53 PM, Chris M. Thomasson wrote:
    On 11/4/2023 12:51 PM, Chris M. Thomasson wrote:
    On 11/4/2023 2:19 AM, Bonita Montero wrote:
    Am 01.11.2023 um 20:59 schrieb Chris M. Thomasson:

    Huh? The benaphore is a semaphore! So, both of you are totally wrong
    here. Think about it...

    Google for "benaphore mutex" and you'll find several refernces that a
    benaphore is a mutex, including the two articles I've referred so far.


    MORON!!!

    The benaphore is a semaphore using a bakery algorithm. You are
    completely confused and incoherently flailing around.

    https://vorbrodt.blog/2019/02/05/fast-semaphore

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Michael S on Sat Nov 4 21:05:36 2023
    On 11/3/2023 7:10 AM, Michael S wrote:
    On Thu, 2 Nov 2023 16:02:04 -0000 (UTC)
    Kaz Kylheku <[email protected]> wrote:

    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 15:58 schrieb Scott Lurndal:

    No. Each is distinct.

    You can initialize a binary semaphore with true and use it
    as a mutex. And a mutex can be realized without a short path
    but just with a binary semaphore in the mentioned way.

    You can use a label, if and goto as a while loop.

    That doesn't mean if and goto /are/ while.

    Speaking of which, I have often mused that sempahores are like the go
    to statement of synchronization---which is ironic, given their
    inventor.


    IMHO, the word 'mutex' has no exact meaning agreed by all or even
    majority of Computer Science community.


    mutual exclusion?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sun Nov 5 05:35:30 2023
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sun Nov 5 05:36:56 2023
    Am 04.11.2023 um 20:44 schrieb Chris M. Thomasson:

    Oh god. The benaphore is a semaphore. ...

    It's a mutex; google for "benaphore mutex -semaphore" and you'll
    see a lot of people telling the same like me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sun Nov 5 05:33:16 2023
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Chris M. Thomasson on Sun Nov 5 04:23:29 2023
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    On 11/3/2023 7:10 AM, Michael S wrote:
    On Thu, 2 Nov 2023 16:02:04 -0000 (UTC)
    Kaz Kylheku <[email protected]> wrote:

    On 2023-11-02, Bonita Montero <[email protected]> wrote:
    Am 02.11.2023 um 15:58 schrieb Scott Lurndal:

    No. Each is distinct.

    You can initialize a binary semaphore with true and use it
    as a mutex. And a mutex can be realized without a short path
    but just with a binary semaphore in the mentioned way.

    You can use a label, if and goto as a while loop.

    That doesn't mean if and goto /are/ while.

    Speaking of which, I have often mused that sempahores are like the go
    to statement of synchronization---which is ironic, given their
    inventor.


    IMHO, the word 'mutex' has no exact meaning agreed by all or even
    majority of Computer Science community.


    mutual exclusion?

    Yeah; you know. I lock it, then somehow cleanly stay out of your way
    while you unlock. Mutual exclusion!

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Branimir Maksimovic@21:1/5 to Bonita Montero on Sun Nov 5 15:30:36 2023
    On 2023-11-04, Bonita Montero <[email protected]> wrote:
    Maybe I missed some header which is implicitly included with me.
    The code compiles fine with g++, clang++, clang-cl and MSVC on
    my PC.

    It compiles, but aborts when running...

    maxa@Branimirs-MacBook-Air News % g++ -O2 run.cpp -o run -std=c++20
    run.cpp:37:2: warning: non-void lambda does not return a value [-Wreturn-type]
    };
    ^
    1 warning generated.
    bmaxa@Branimirs-MacBook-Air News % ./run
    libc++abi: terminating
    zsh: abort ./run




    --

    7-77-777, Evil Sinner! https://www.linkedin.com/in/branimir-maksimovic-6762bbaa/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Sun Nov 5 18:55:00 2023
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Sun Nov 5 17:25:22 2023
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sun Nov 5 12:11:31 2023
    On 11/4/2023 9:35 PM, Bonita Montero wrote:
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    Any semaphore can be used as a binary semaphore, which can in turn be
    used for mutual exclusion. Now, the mutex logic itself can record what
    thread has ownership, ect... This is separate from the semaphore logic.
    Are you sure that you are not misunderstanding me?

    A semaphore does not have a concept of ownership. However, a mutex does.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sun Nov 5 12:12:48 2023
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. You, along with the "experts"
    that try to say that a benaphore is a mutex are 100% completely wrong.
    Got it?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sun Nov 5 12:15:42 2023
    On 11/4/2023 12:54 PM, Chris M. Thomasson wrote:
    On 11/4/2023 12:53 PM, Chris M. Thomasson wrote:
    On 11/4/2023 12:51 PM, Chris M. Thomasson wrote:
    On 11/4/2023 2:19 AM, Bonita Montero wrote:
    Am 01.11.2023 um 20:59 schrieb Chris M. Thomasson:

    Huh? The benaphore is a semaphore! So, both of you are totally
    wrong here. Think about it...

    Google for "benaphore mutex" and you'll find several refernces that a
    benaphore is a mutex, including the two articles I've referred so far. >>>>

    MORON!!!

    The benaphore is a semaphore using a bakery algorithm. You are
    completely confused and incoherently flailing around.

    https://vorbrodt.blog/2019/02/05/fast-semaphore

    Fwiw, Joe Seigh created a really interesting read/write ticket based
    bakery algorithm a long time ago back in comp.programming.threads. It
    was a spinlock, but the logic was rather elegant. Humm... I wonder if I
    can find that old thread!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Sun Nov 5 20:53:53 2023
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.

    Firstly, "I googled this up" to "experts" is a huge leap.

    Secondly, when have you ever recognized or acknowledged expertise?

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sun Nov 5 12:18:08 2023
    On 11/5/2023 12:12 PM, Chris M. Thomasson wrote:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. You, along with the "experts"
    that try to say that a benaphore is a mutex are 100% completely wrong.
    Got it?

    Actually, an "expert" that tries to say that a benaphore is a mutex
    should be FIRED on the spot.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Chris M. Thomasson on Sun Nov 5 22:55:37 2023
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    Actually, an "expert" that tries to say that a benaphore is a mutex
    should be FIRED on the spot.

    They probably are; just not from the Google search index.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Chris M. Thomasson on Sun Nov 5 22:54:51 2023
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    On 11/4/2023 9:35 PM, Bonita Montero wrote:
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    Any semaphore can be used as a binary semaphore, which can in turn be
    used for mutual exclusion. Now, the mutex logic itself can record what
    thread has ownership, ect... This is separate from the semaphore logic.
    Are you sure that you are not misunderstanding me?

    It's like someone paid to misunderstand ... only minus any actual money.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Sun Nov 5 18:53:22 2023
    On 11/5/2023 2:55 PM, Kaz Kylheku wrote:
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    Actually, an "expert" that tries to say that a benaphore is a mutex
    should be FIRED on the spot.

    They probably are; just not from the Google search index.


    Wow. Yup. Well, shit happens. Your company hired Bontia, and he starts
    trying to tell everybody that a benaphore is a mutex... Well... This a
    place for a highly sarcastic comic I have been thinking about creating
    called "Codan the Barbarian". If Codan heard Bonita say that, the ring
    of skulls he carries around would start glowing white hot... Then Codan
    would flip out. Codan is about 7 feet tall and around 340 pounds, not an
    ounce of fat. He happens to be a talented programmer.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Sun Nov 5 18:49:36 2023
    On 11/5/2023 2:54 PM, Kaz Kylheku wrote:
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    On 11/4/2023 9:35 PM, Bonita Montero wrote:
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    Any semaphore can be used as a binary semaphore, which can in turn be
    used for mutual exclusion. Now, the mutex logic itself can record what
    thread has ownership, ect... This is separate from the semaphore logic.
    Are you sure that you are not misunderstanding me?

    It's like someone paid to misunderstand ... only minus any actual money.


    Humm... Interesting comment! Almost hard to disagree with wrt the, Bontia...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Chris M. Thomasson on Mon Nov 6 02:58:46 2023
    On 2023-11-06, Chris M. Thomasson <[email protected]> wrote:
    On 11/5/2023 2:54 PM, Kaz Kylheku wrote:
    On 2023-11-05, Chris M. Thomasson <[email protected]> wrote:
    On 11/4/2023 9:35 PM, Bonita Montero wrote:
    Am 04.11.2023 um 20:42 schrieb Chris M. Thomasson:

    The surrounding code is the mutex. ...

    You didn't understand me. The surrounding code that operates
    on the guarded data structure isn't the mutex.

    Any semaphore can be used as a binary semaphore, which can in turn be
    used for mutual exclusion. Now, the mutex logic itself can record what
    thread has ownership, ect... This is separate from the semaphore logic.
    Are you sure that you are not misunderstanding me?

    It's like someone paid to misunderstand ... only minus any actual money.

    Humm... Interesting comment! Almost hard to disagree with wrt the, Bontia...

    It's a variation on a quote from Upton Sinclair, evidently found in
    _I, Candidate for Governor: And How I Got Licked_ [1935]

    "It is difficult to get a man to understand something when his salary
    depends on his not understanding it."

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sun Nov 5 20:10:12 2023
    On 11/5/2023 7:44 PM, Bonita Montero wrote:
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. ...

    If you take it like that every mutex is also a binary semaphore.



    Nope. Wow, you massively confused! A mutex does not need to use
    semaphore logic, it can have its own fast path algorithm, XCHG for
    instance. However, it can use a semaphore for its slow paths. You are
    being radically obtuse here. Wow.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 04:44:55 2023
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. ...

    If you take it like that every mutex is also a binary semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 05:15:40 2023
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. You, along with the "experts"
    that try to say that a benaphore is a mutex are 100% completely wrong.
    Got it?

    If you take it that way every mutex is a binary seamphore;
    but no one refers to a mutex as a semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 05:16:35 2023
    Am 06.11.2023 um 05:10 schrieb Chris M. Thomasson:

    Nope. Wow, you massively confused! A mutex does not need to use
    semaphore logic, it can have its own fast path algorithm, XCHG for
    instance. ...

    With pure userland-logic you won't have the opportunity to sleep for
    a mutex or to be awakened.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 05:37:15 2023
    Am 06.11.2023 um 05:22 schrieb Kaz Kylheku:

    Umm, nope! Because you can't use a mutex, for instance, in
    signaling "item available" in a producer consumer queue.

    So can you make a producer-consumer-queue with a benaphore ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Mon Nov 6 04:22:49 2023
    On 2023-11-06, Bonita Montero <[email protected]> wrote:
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. ...

    If you take it like that every mutex is also a binary semaphore.

    Umm, nope! Because you can't use a mutex, for instance, in
    signaling "item available" in a producer consumer queue.

    You can't have a producer doing nothing but unlock(mutex) to signal that
    it has added an item, and a consumer doing nothing but lock(mutex).

    If that happens to work, then we know the mutex is really a semaphore.

    If it works, but the documentation says that the behavior is undefined,
    we probably shouldn't do that, but use something that is documented.

    No mutex provided in any mainstream operating system supports this
    as a documented case that is required to work.

    No implementation of POSIX mutexes, not Windows' CRITICAL_SECTION, not
    Win32 Mutexes, not C++ or ISO C mutexes.

    You don't care that something is undefined because you're a professional
    troll, not a professional developer.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Sun Nov 5 21:00:15 2023
    On 11/5/2023 8:22 PM, Kaz Kylheku wrote:
    On 2023-11-06, Bonita Montero <[email protected]> wrote:
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. ...

    If you take it like that every mutex is also a binary semaphore.

    Umm, nope! Because you can't use a mutex, for instance, in
    signaling "item available" in a producer consumer queue.

    You can't have a producer doing nothing but unlock(mutex) to signal that
    it has added an item, and a consumer doing nothing but lock(mutex).

    If that happens to work, then we know the mutex is really a semaphore.

    If it works, but the documentation says that the behavior is undefined,
    we probably shouldn't do that, but use something that is documented.

    No mutex provided in any mainstream operating system supports this
    as a documented case that is required to work.

    No implementation of POSIX mutexes, not Windows' CRITICAL_SECTION, not
    Win32 Mutexes, not C++ or ISO C mutexes.

    You don't care that something is undefined because you're a professional troll, not a professional developer.


    Check out this bakery algorithm I did wrt a multi producer consumer queue:

    https://groups.google.com/g/lock-free/c/acjQ3-89abE/m/a6-Di0GZsyEJ

    Pretty nice, and can be used with futexes to get rid of the spins.

    <pseudo code, membars aside for a moment> ______________________________________________
    struct cell { uint32_t ver; double state; };

    uint32_t head = 0;
    uint32_t tail = 0;
    cell cells[N]; // N must be a power of 2

    void init() {
    for (uint32_t i = 0; i < N; ++i) cells[i].ver = i;
    }

    void producer(double state) {
    uint32_t ver = XADD(&head, 1);
    cell& c = cells[ver & (N - 1)];
    while (LOAD(&c.ver) != ver) backoff();
    c.state = state;
    STORE(&c.ver, ver + 1);
    }

    double consumer() {
    uint32_t ver = XADD(&tail, 1);
    cell& c = cells[ver & (N - 1)];
    while (LOAD(&c.ver) != ver + 1) backoff();
    double state = c.state;
    STORE(&c.ver, ver + N);
    return state;
    }
    ______________________________________________

    Pretty elegant logic...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sun Nov 5 21:01:37 2023
    On 11/5/2023 8:37 PM, Bonita Montero wrote:
    Am 06.11.2023 um 05:22 schrieb Kaz Kylheku:

    Umm, nope! Because you can't use a mutex, for instance, in
    signaling "item available" in a producer consumer queue.

    So can you make a producer-consumer-queue with a benaphore ?

    Huh? You can use a semaphore to allow a multi producer consumer queue to
    wait in the kernel for its slow paths. This is rather basic Bonita. wow.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Sun Nov 5 21:02:14 2023
    On 11/5/2023 8:15 PM, Bonita Montero wrote:
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. You, along with the "experts"
    that try to say that a benaphore is a mutex are 100% completely wrong.
    Got it?

    If you take it that way every mutex is a binary seamphore;
    but no one refers to a mutex as a semaphore.


    Wtf are you babbling on about!?!?!!?!?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Sun Nov 5 21:05:19 2023
    On 11/5/2023 9:02 PM, Chris M. Thomasson wrote:
    On 11/5/2023 8:15 PM, Bonita Montero wrote:
    Am 05.11.2023 um 21:12 schrieb Chris M. Thomasson:
    On 11/5/2023 9:55 AM, Bonita Montero wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.


    Sorry buddy! A benaphore is a semaphore. You, along with the
    "experts" that try to say that a benaphore is a mutex are 100%
    completely wrong. Got it?

    If you take it that way every mutex is a binary seamphore;
    but no one refers to a mutex as a semaphore.


    Wtf are you babbling on about!?!?!!?!?

    A mutex is not a semaphore. A semaphore is not a mutex.

    :^)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 09:37:51 2023
    Am 06.11.2023 um 06:05 schrieb Chris M. Thomasson:

    A mutex is not a semaphore. A semaphore is not a mutex.

    A mutex which doesn't maintain the identity of the locking
    thread also can be used as a semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 09:36:28 2023
    Am 06.11.2023 um 06:00 schrieb Chris M. Thomasson:

    void producer(double state) {
        uint32_t ver = XADD(&head, 1);
        cell& c = cells[ver & (N - 1)];
        while (LOAD(&c.ver) != ver) backoff();
        c.state = state;
        STORE(&c.ver, ver + 1);
    }

    double consumer() {
        uint32_t ver = XADD(&tail, 1);
        cell& c = cells[ver & (N - 1)];
        while (LOAD(&c.ver) != ver + 1) backoff();
        double state = c.state;
        STORE(&c.ver, ver + N);
        return state;
    }

    Lock-free queues are more complex and even if they're implemented
    correctly like with boost they suck because they're polled. They
    for sure don't make it into the standard.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Kaz Kylheku on Mon Nov 6 12:13:57 2023
    On 11/5/2023 12:53 PM, Kaz Kylheku wrote:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    Am 05.11.2023 um 18:25 schrieb Kaz Kylheku:
    On 2023-11-05, Bonita Montero <[email protected]> wrote:
    google for ... and you'll see a lot of people telling the same
    like me.

    Bandwagon Fallacy

    https://en.wikipedia.org/wiki/Argumentum_ad_populum

    In most cases experts are right if they agree on a certain fact.

    Firstly, "I googled this up" to "experts" is a huge leap.

    Secondly, when have you ever recognized or acknowledged expertise?


    Bonita is the expert? Wow... ;^o

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Mon Nov 6 12:11:35 2023
    On 11/6/2023 12:36 AM, Bonita Montero wrote:
    Am 06.11.2023 um 06:00 schrieb Chris M. Thomasson:

    void producer(double state) {
         uint32_t ver = XADD(&head, 1);
         cell& c = cells[ver & (N - 1)];
         while (LOAD(&c.ver) != ver) backoff();
         c.state = state;
         STORE(&c.ver, ver + 1);
    }

    double consumer() {
         uint32_t ver = XADD(&tail, 1);
         cell& c = cells[ver & (N - 1)];
         while (LOAD(&c.ver) != ver + 1) backoff();
         double state = c.state;
         STORE(&c.ver, ver + N);
         return state;
    }

    Lock-free queues are more complex and even if they're implemented
    correctly like with boost they suck because they're polled.

    What do you mean by that? Why do you _seem_ to think that a lock-free
    queue cannot have slow-paths that wait in the kernel?


    They
    for sure don't make it into the standard.

    I can get rid of the spin wrt the tickets using futexs. The queue logic
    is what I find rather elegant here. Also, I can use a benaphore for
    waiting logic. This bounded lock-free queue is pretty damn good.

    Btw, there are different queues types.

    multi-producer multi-consumer
    single-producer multi-consumer

    multi-producer single-consumer
    single-producer single-consumer

    All of them have their trade offs, and they have radically different
    algorithms to implement them efficiently.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Mon Nov 6 12:13:20 2023
    On 11/6/2023 12:37 AM, Bonita Montero wrote:
    Am 06.11.2023 um 06:05 schrieb Chris M. Thomasson:

    A mutex is not a semaphore. A semaphore is not a mutex.

    A mutex which doesn't maintain the identity of the locking
    thread also can be used as a semaphore.



    Barf! A mutex is not a semaphore. However, you can implement a semaphore
    using a mutex and condition variables.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 23:52:34 2023
    Am 06.11.2023 um 21:11 schrieb Chris M. Thomasson:

    What do you mean by that? Why do you _seem_ to think that a lock-free
    queue cannot have slow-paths that wait in the kernel?

    If it is lock-free there's no lock-free any more.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Mon Nov 6 23:53:15 2023
    Am 06.11.2023 um 21:13 schrieb Chris M. Thomasson:

    Barf! A mutex is not a semaphore. ...

    A mutex which doesn't maintain the caller's identity is a semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Mon Nov 6 23:00:13 2023
    On 2023-11-06, Bonita Montero <[email protected]> wrote:
    Am 06.11.2023 um 21:13 schrieb Chris M. Thomasson:

    Barf! A mutex is not a semaphore. ...

    A mutex which doesn't maintain the caller's identity is a semaphore.

    We cannot jump to that conclusion.

    To use a mutex a a semaphore, we map the unlock operation to signal
    and lock to wait.

    It must be possible to signal multiple times.

    If the mutex object rejects the unlock operation if it is already
    in an unlocked state (for instance, by terminating the process with an assertion) then it cannot function as a semaphore.

    I suggest you abandon this fruitless exercise and let semaphores
    be semaphores and mutexes be mutexes.

    They are different things and we have different words for them,
    which is useful; don't try to wreck it.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 05:36:13 2023
    Am 07.11.2023 um 00:00 schrieb Kaz Kylheku:

    To use a mutex a a semaphore, we map the unlock operation to signal
    and lock to wait.

    If you have a mutex that doesn't memorize who's the locking thread
    a lock() is an acquire() and an unlock() is a release().

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Tue Nov 7 05:29:16 2023
    On 2023-11-07, Bonita Montero <[email protected]> wrote:
    Am 07.11.2023 um 00:00 schrieb Kaz Kylheku:

    To use a mutex a a semaphore, we map the unlock operation to signal
    and lock to wait.

    If you have a mutex that doesn't memorize who's the locking thread
    a lock() is an acquire() and an unlock() is a release().

    That does not follow. Sure; not memorizing the locking thread
    does remove one roadblock.

    Not having an owner appears to be is a necessary condition.

    You're not distinguishing between necessary and sufficient conditions.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 09:38:50 2023
    Am 07.11.2023 um 06:29 schrieb Kaz Kylheku:

    That does not follow. Sure; not memorizing the locking thread
    does remove one roadblock.
    Not having an owner appears to be is a necessary condition.
    You're not distinguishing between necessary and sufficient conditions.

    It the mutex doesn't track the owner, specified or not,
    it's a binary semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Tue Nov 7 11:53:13 2023
    On 11/7/2023 12:38 AM, Bonita Montero wrote:
    Am 07.11.2023 um 06:29 schrieb Kaz Kylheku:

    That does not follow. Sure; not memorizing the locking thread
    does remove one roadblock.
    Not having an owner appears to be is a necessary condition.
    You're not distinguishing between necessary and sufficient conditions.

    It the mutex doesn't track the owner, specified or not,
    it's a binary semaphore.

    Still wrong.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Tue Nov 7 11:51:45 2023
    On 11/6/2023 2:52 PM, Bonita Montero wrote:
    Am 06.11.2023 um 21:11 schrieb Chris M. Thomasson:

    What do you mean by that? Why do you _seem_ to think that a lock-free
    queue cannot have slow-paths that wait in the kernel?

    If it is lock-free there's no lock-free any more.



    It is basically morphing any lock-free algorithm into something that has fast-paths that avoid the kernel, and slow paths the make use of the
    kernel. Imho, it's interesting to do.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to David Brown on Wed Nov 8 20:33:24 2023
    On 11/3/2023 2:13 AM, David Brown wrote:
    On 02/11/2023 21:12, Chris M. Thomasson wrote:
    On 11/2/2023 8:34 AM, David Brown wrote:
    On 02/11/2023 15:58, Scott Lurndal wrote:
    Bonita Montero <[email protected]> writes:
    Am 01.11.2023 um 20:58 schrieb Chris M. Thomasson:

    The benaphore is a semaphore not a mutex! Fwiw, you can use any
    semaphore as a binary semaphore for use as a mutex. Got it?

    A binary semaphore is also a mutex.

    No.  Each is distinct.

    It is correct, however, to say that both provide the same
    functionality.


    Both provide /similar/ functionality, but not necessarily the same.
    Different OS's differ in the details, but it is common for mutexes to
    have priority boosting if a high priority thread is waiting for a
    mutex currently held by a low priority thread.  Such boosting is
    normally not part of the functionality of a binary semaphore.  This
    makes them very distinctly different on such OS's - a mutex is a
    lock, a semaphore (binary or otherwise) is a signalling mechanism.

    Right. Basically, any thread/process can increment and/or decrement a
    semaphore. This is different than pure mutex logic. Even if the mutex
    uses a semaphore under the covers...


    That's a very different issue - though of course it is an equally
    important distinction between mutexes and binary semaphores.  (And no, a mutex cannot be implemented using semaphores if the mutex has protection against priority inversion, and the semaphore does not.)


    (I don't know if Windows makes such a distinction - it's not an OS
    you use when thread or process priority is important.  It is
    certainly common, though not universal, for RTOS's.  And while I
    don't know the details, I'd be surprised if Linux and other *nix's
    didn't have such a distinction.)




    Got to get very intimate, like how a mutex is very intimate wrt a
    condition variable... ;^)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 20:44:48 2023
    On 11/5/2023 8:16 PM, Bonita Montero wrote:
    Am 06.11.2023 um 05:10 schrieb Chris M. Thomasson:

    Nope. Wow, you massively confused! A mutex does not need to use
    semaphore logic, it can have its own fast path algorithm, XCHG for
    instance. ...

    With pure userland-logic you won't have the opportunity to sleep for
    a mutex or to be awakened.

    I want to say Bontia == Dr. Trolls-a-LOT?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 20:45:13 2023
    On 11/7/2023 12:38 AM, Bonita Montero wrote:
    Am 07.11.2023 um 06:29 schrieb Kaz Kylheku:

    That does not follow. Sure; not memorizing the locking thread
    does remove one roadblock.
    Not having an owner appears to be is a necessary condition.
    You're not distinguishing between necessary and sufficient conditions.

    It the mutex doesn't track the owner, specified or not,
    it's a binary semaphore.



    https://youtu.be/cvg5mbM6FGs?t=161

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 9 05:49:19 2023
    Am 07.11.2023 um 20:53 schrieb Chris M. Thomasson:

    On 11/7/2023 12:38 AM, Bonita Montero wrote:

    It the mutex doesn't track the owner, specified or not,
    it's a binary semaphore.

    Still wrong.

    No, absolutely true. If you have a mutex which doesn't track
    the owning thread you're not mandated to release it in the
    same thread.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 20:52:36 2023
    On 11/8/2023 8:49 PM, Bonita Montero wrote:
    Am 07.11.2023 um 20:53 schrieb Chris M. Thomasson:

    On 11/7/2023 12:38 AM, Bonita Montero wrote:

    It the mutex doesn't track the owner, specified or not,
    it's a binary semaphore.

    Still wrong.

    No, absolutely true. If you have a mutex which doesn't track
    the owning thread you're not mandated to release it in the
    same thread.


    Mutex != Semaphore

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 9 05:54:25 2023
    Am 09.11.2023 um 05:52 schrieb Chris M. Thomasson:

    Mutex != Semaphore

    A mutex which doesn't track the owning thread is functionally
    a semaphore.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 20:57:21 2023
    On 11/8/2023 8:54 PM, Bonita Montero wrote:
    Am 09.11.2023 um 05:52 schrieb Chris M. Thomasson:

    Mutex != Semaphore

    A mutex which doesn't track the owning thread is functionally
    a semaphore.

    Barf! I almost puked. Thanks, Bonita. ;^o

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Chris M. Thomasson on Thu Nov 9 01:05:32 2023
    On 11/4/2023 11:44 AM, Chris M. Thomasson wrote:
    On 11/4/2023 8:53 AM, Scott Lurndal wrote:
    "Chris M. Thomasson" <[email protected]> writes:
    On 11/3/2023 3:13 PM, Chris M. Thomasson wrote:
    On 11/3/2023 5:36 AM, Bonita Montero wrote:
    Am 01.11.2023 um 21:02 schrieb Chris M. Thomasson:

    If you can avoid using a CAS, aka CMPXCHG over on intel, then do it! >>>>>> Although, wrt intel, there is a way to use CMPXCHG without looping. >>>>>
    I don't believe that, give me some code-example _here_. The expected >>>>> value of a compare exchange might always have been asynchronously
    changed, so you need looping every time.

    Think, strong CAS vs weak CAS...

    Strong CAS vs. weak CAS only makes a difference of architectures with >>>>> LL/SC. With these architectures a CAS may fail although the actual
    data corresponds with the expected value because the cacheline might >>>>> have been evicted in the meantime. compare_exchange_strong checks for >>>>> this case and loops internally, whereas compare_exchange_weak gives a >>>>> false result after one try.

    Strong CAS cannot fail spuriously. Put on your thinking cap.


    CMPXCHG cannot fail spuriously. However, LL/SC can.

    LL/SC is not compare and swap.



    LL/SC can be used to implement a weak CAS. Also, wrt the algorithm, it
    can be used to implement a strong CAS.

    CMPXCHG over on intel cannot be used to implement a weak CAS.

    Well, there is a "way" wrt the algorithm that can mimic weak CAS even
    with CMPXCHG. It involves checking the comparand before using CMPXCHG.
    This would not be ideal, but it can be done. I would stay far away from
    it. I do not really like optimistic sync, like LL/SC. Imvvho, CMPXCHG is
    more, pessimistic, so to speak...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)