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


Groups > comp.lang.c++ > #89009 > unrolled thread

timed_mutex -- but it acquires the lock

Started byFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
First post2023-02-07 01:25 -0800
Last post2023-02-09 14:17 -0800
Articles 20 on this page of 47 — 9 participants

Back to article view | Back to comp.lang.c++


Contents

  timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-07 01:25 -0800
    Re: timed_mutex -- but it acquires the lock Muttley@dastardlyhq.com - 2023-02-07 09:40 +0000
      Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 12:12 -0800
    Re: timed_mutex -- but it acquires the lock Bo Persson <bo@bo-persson.se> - 2023-02-07 10:48 +0100
      Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-07 05:46 -0800
        Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 12:14 -0800
      Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 12:22 -0800
        Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-07 14:17 -0800
          Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 14:24 -0800
            Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 14:49 -0800
            Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-07 14:51 -0800
              Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 14:53 -0800
                Re: timed_mutex -- but it acquires the lock scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 23:01 +0000
                  Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 16:06 -0800
          Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 14:28 -0800
    Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-07 12:23 +0200
    Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 14:53 -0800
    Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 16:08 -0800
      Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-08 10:37 -0800
        Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-08 20:52 +0200
          Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-08 12:37 -0800
          Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-08 15:14 -0800
            Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-08 17:33 -0800
            Re: timed_mutex -- but it acquires the lock Öö Tiib <ootiib@hot.ee> - 2023-02-08 17:45 -0800
            Re: timed_mutex -- but it acquires the lock David Brown <david.brown@hesbynett.no> - 2023-02-09 09:23 +0100
              Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-09 01:24 -0800
                Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-09 02:02 -0800
                  Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 15:10 -0800
                    Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-10 08:06 -0800
                      Re: timed_mutex -- but it acquires the lock David Brown <david.brown@hesbynett.no> - 2023-02-10 17:30 +0100
                        Re: timed_mutex -- but it acquires the lock scott@slp53.sl.home (Scott Lurndal) - 2023-02-10 17:02 +0000
                          Re: timed_mutex -- but it acquires the lock Richard Damon <Richard@Damon-Family.org> - 2023-02-10 20:02 -0500
                            Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-17 12:58 -0800
                      Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-10 20:57 +0200
            Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 04:02 -0800
              Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-09 04:16 -0800
                Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 04:28 -0800
                Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-09 14:36 +0200
                  Re: timed_mutex -- but it acquires the lock Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-02-09 08:34 -0800
                    Re: timed_mutex -- but it acquires the lock David Brown <david.brown@hesbynett.no> - 2023-02-09 20:25 +0100
                      Re: timed_mutex -- but it acquires the lock scott@slp53.sl.home (Scott Lurndal) - 2023-02-09 19:39 +0000
                    Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-09 23:24 +0200
                      Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 14:08 -0800
                      Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 15:07 -0800
        Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-08 12:36 -0800
        Re: timed_mutex -- but it acquires the lock Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-09 09:14 +0200
          Re: timed_mutex -- but it acquires the lock "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 14:17 -0800

Page 1 of 3  [1] 2 3  Next page →


#89009 — timed_mutex -- but it acquires the lock

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-07 01:25 -0800
Subjecttimed_mutex -- but it acquires the lock
Message-ID<64e83b9b-70d8-4273-9423-f7e7f96b453en@googlegroups.com>
Since C++11, we already have "std::timed_mutex". It will try to acquire the lock for N seconds and fail, and you can handle the failure.

I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.

Last week I posted code here for a mutex that could be unlocked by a thread other than the one that locked it:

    https://groups.google.com/g/comp.lang.c++/c/HtUKQlNUI50

Similarly, I would use an "std::atomic_flag" as a mutex that can be unlocked by any thread.

A well-written program should work properly 99.9% of the time. There will always be unforeseen problems like a hardware failure, or a yet-to-be-discovered bug in a third-party library, or an operating system bug, or another process is set to a high priority with high CPU usage. This is why, when I'm finished writing a program that works very well, I try to add extra robustness to it in order to try make it recover gracefully from unforeseen errors. This means I might add in code to kill a thread that has become unresponsive. This is why I'm thinking I should write a 'timed_rescuable_mutex". In my code I would then use it with "std::unique_lock::try_lock_for".

[toc] | [next] | [standalone]


#89010

FromMuttley@dastardlyhq.com
Date2023-02-07 09:40 +0000
Message-ID<trt69j$3h527$1@dont-email.me>
In reply to#89009
On Tue, 7 Feb 2023 01:25:11 -0800 (PST)
Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> wrote:
>Since C++11, we already have "std::timed_mutex". It will try to acquire the=
> lock for N seconds and fail, and you can handle the failure.
>
>I want to make another kind of timed mutex, called something like "std::tim=
>ed_rescuable_mutex", but the difference will be that when you invoke "try_l=
>ock_for", it will wait for N seconds, and then if the mutex is still locked=
>, it will kill the thread that locked it, and then unlock the mutex.
>
>Last week I posted code here for a mutex that could be unlocked by a thread=
> other than the one that locked it:
>
>    https://groups.google.com/g/comp.lang.c++/c/HtUKQlNUI50
>
>Similarly, I would use an "std::atomic_flag" as a mutex that can be unlocke=
>d by any thread.

Huh? If any thread can unlock a mutex then whats the point of having it in
the first place?

[toc] | [prev] | [next] | [standalone]


#89024

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 12:12 -0800
Message-ID<trubca$3rom8$4@dont-email.me>
In reply to#89010
On 2/7/2023 1:40 AM, Muttley@dastardlyhq.com wrote:
> On Tue, 7 Feb 2023 01:25:11 -0800 (PST)
> Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> wrote:
>> Since C++11, we already have "std::timed_mutex". It will try to acquire the=
>> lock for N seconds and fail, and you can handle the failure.
>>
>> I want to make another kind of timed mutex, called something like "std::tim=
>> ed_rescuable_mutex", but the difference will be that when you invoke "try_l=
>> ock_for", it will wait for N seconds, and then if the mutex is still locked=
>> , it will kill the thread that locked it, and then unlock the mutex.
>>
>> Last week I posted code here for a mutex that could be unlocked by a thread=
>> other than the one that locked it:
>>
>>     https://groups.google.com/g/comp.lang.c++/c/HtUKQlNUI50
>>
>> Similarly, I would use an "std::atomic_flag" as a mutex that can be unlocke=
>> d by any thread.
> 
> Huh? If any thread can unlock a mutex then whats the point of having it in
> the first place?
> 

The thread that locks a mutex MUST be the thread that unlocks it. 
Period. If you need something different than that, use a binary 
semaphore or something.

[toc] | [prev] | [next] | [standalone]


#89011

FromBo Persson <bo@bo-persson.se>
Date2023-02-07 10:48 +0100
Message-ID<k4el2qF5ktqU1@mid.individual.net>
In reply to#89009
On 2023-02-07 at 10:25, Frederick Virchanza Gotham wrote:
> 
> Since C++11, we already have "std::timed_mutex". It will try to acquire the lock for N seconds and fail, and you can handle the failure.
> 
> I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.
> 

Killing a thread at a random position seems to be a perfect way to enter 
an unspecified state for the program.

What if that thread is halfway through reallocating a vector?

[toc] | [prev] | [next] | [standalone]


#89013

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-07 05:46 -0800
Message-ID<d7bc50b5-cce2-4bea-a161-e2ddcdcfde56n@googlegroups.com>
In reply to#89011
On Tuesday, February 7, 2023 at 9:48:24 AM UTC, Bo Persson wrote:
>
> Killing a thread at a random position seems to be a perfect way to enter 
> an unspecified state for the program. 
> 
> What if that thread is halfway through reallocating a vector?

> Paavo Helde wrote:
>
> So if our customer wants to analyze yet another batch of their 100,000
> microscope images, the program would randomly fail on 100 images. Not a
> great selling point.


I would consider what the estimated time is for the task. If one thread is processing 800 images, and if each image should take between 7 and 9 milliseconds, then the whole lot should take between 5.6 seconds and 7.2 seconds. After 10 seconds I can check if the thread is still responsive. After another 5 seconds I can kill it and discard all of its work.

[toc] | [prev] | [next] | [standalone]


#89025

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 12:14 -0800
Message-ID<truben$3rom8$5@dont-email.me>
In reply to#89013
On 2/7/2023 5:46 AM, Frederick Virchanza Gotham wrote:
> On Tuesday, February 7, 2023 at 9:48:24 AM UTC, Bo Persson wrote:
>>
>> Killing a thread at a random position seems to be a perfect way to enter
>> an unspecified state for the program.
>>
>> What if that thread is halfway through reallocating a vector?
> 
>> Paavo Helde wrote:
>>
>> So if our customer wants to analyze yet another batch of their 100,000
>> microscope images, the program would randomly fail on 100 images. Not a
>> great selling point.
> 
> 
> I would consider what the estimated time is for the task. If one thread is processing 800 images, and if each image should take between 7 and 9 milliseconds, then the whole lot should take between 5.6 seconds and 7.2 seconds. After 10 seconds I can check if the thread is still responsive. After another 5 seconds I can kill it and discard all of its work.
> 
> 

Humm... Are you just starting out with threading?

[toc] | [prev] | [next] | [standalone]


#89026

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 12:22 -0800
Message-ID<trubts$3rom8$6@dont-email.me>
In reply to#89011
On 2/7/2023 1:48 AM, Bo Persson wrote:
> On 2023-02-07 at 10:25, Frederick Virchanza Gotham wrote:
>>
>> Since C++11, we already have "std::timed_mutex". It will try to 
>> acquire the lock for N seconds and fail, and you can handle the failure.
>>
>> I want to make another kind of timed mutex, called something like 
>> "std::timed_rescuable_mutex", but the difference will be that when you 
>> invoke "try_lock_for", it will wait for N seconds, and then if the 
>> mutex is still locked, it will kill the thread that locked it, and 
>> then unlock the mutex.
>>
> 
> Killing a thread at a random position seems to be a perfect way to enter 
> an unspecified state for the program.

It's horrible. Well, I had to deal with robust mutexes many moons ago. 
If a processed dies while holding a mutex, we can try to recover and 
repair its state:

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

Windows even has WAIT_ABANDONED.

Have fun! It can be done, but is it not exactly, fun...


> 
> What if that thread is halfway through reallocating a vector?
> 

Yikes!

[toc] | [prev] | [next] | [standalone]


#89028

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-07 14:17 -0800
Message-ID<bd7cf957-c436-4739-adeb-30808de14393n@googlegroups.com>
In reply to#89026
On Tuesday, February 7, 2023 at 8:22:37 PM UTC, Chris M. Thomasson wrote:

> It's horrible. Well, I had to deal with robust mutexes many moons ago. 


I've worked with robust mutexes in Linux; if the thread that acquired the lock dies, the lock is released.

However I'm trying to accommodate a situation in which a worker thread freezes -- the thread's still alive but it's caught in an infinite loop or a blocking function won't return.

In your previous post you said that the thread that unlocks a mutex must be that one had locked it ... well I'll use an atomic_flag like I described in the link I gave in my original post.

[toc] | [prev] | [next] | [standalone]


#89029

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 14:24 -0800
Message-ID<truj2v$3tacv$1@dont-email.me>
In reply to#89028
On 2/7/2023 2:17 PM, Frederick Virchanza Gotham wrote:
> On Tuesday, February 7, 2023 at 8:22:37 PM UTC, Chris M. Thomasson wrote:
> 
>> It's horrible. Well, I had to deal with robust mutexes many moons ago.
> 
> 
> I've worked with robust mutexes in Linux; if the thread that acquired the lock dies, the lock is released.

No. Not threads. Processes. If a thread dies in a process it will take 
down the whole process.


> 
> However I'm trying to accommodate a situation in which a worker thread freezes -- the thread's still alive but it's caught in an infinite loop or a blocking function won't return.
> 
> In your previous post you said that the thread that unlocks a mutex must be that one had locked it ... well I'll use an atomic_flag like I described in the link I gave in my original post.

[toc] | [prev] | [next] | [standalone]


#89031

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 14:49 -0800
Message-ID<trukho$3tgjl$1@dont-email.me>
In reply to#89029
On 2/7/2023 2:24 PM, Chris M. Thomasson wrote:
> On 2/7/2023 2:17 PM, Frederick Virchanza Gotham wrote:
>> On Tuesday, February 7, 2023 at 8:22:37 PM UTC, Chris M. Thomasson wrote:
>>
>>> It's horrible. Well, I had to deal with robust mutexes many moons ago.
>>
>>
>> I've worked with robust mutexes in Linux; if the thread that acquired 
>> the lock dies, the lock is released.
> 
> No. Not threads. Processes. If a thread dies in a process it will take 
> down the whole process.
> 
> 
>>
>> However I'm trying to accommodate a situation in which a worker thread 
>> freezes -- the thread's still alive but it's caught in an infinite 
>> loop or a blocking function won't return.
[...]

That is a lot different than a thread up and crashing in a process. Why 
do you think a mutex would help you with that? Something reeks of a bad 
design. Using robust mutexes for a single process does not make any 
sense at all.

[toc] | [prev] | [next] | [standalone]


#89032

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-07 14:51 -0800
Message-ID<d5f9db9b-53c6-42fe-bb16-0b527ec3089fn@googlegroups.com>
In reply to#89029
On Tuesday, February 7, 2023 at 10:24:47 PM UTC, Chris M. Thomasson wrote:

> No. Not threads. Processes. If a thread dies in a process it will take 
> down the whole process.

I used the following function:

       https://man7.org/linux/man-pages/man3/pthread_mutexattr_setrobust.3.html

Here's an excerpt:

       "The robustness attribute specifies the behavior of the mutex when the owning thread dies without unlocking the mutex."

[toc] | [prev] | [next] | [standalone]


#89034

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 14:53 -0800
Message-ID<trukpo$3tgjl$3@dont-email.me>
In reply to#89032
On 2/7/2023 2:51 PM, Frederick Virchanza Gotham wrote:
> On Tuesday, February 7, 2023 at 10:24:47 PM UTC, Chris M. Thomasson wrote:
> 
>> No. Not threads. Processes. If a thread dies in a process it will take
>> down the whole process.
> 
> I used the following function:
> 
>         https://man7.org/linux/man-pages/man3/pthread_mutexattr_setrobust.3.html
> 
> Here's an excerpt:
> 
>         "The robustness attribute specifies the behavior of the mutex when the owning thread dies without unlocking the mutex."

If the owning thread dies, it means the process has died. robust mutexes 
are for inter process sync.

[toc] | [prev] | [next] | [standalone]


#89035

Fromscott@slp53.sl.home (Scott Lurndal)
Date2023-02-07 23:01 +0000
Message-ID<eLAEL.662754$vBI8.580486@fx15.iad>
In reply to#89034
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>On 2/7/2023 2:51 PM, Frederick Virchanza Gotham wrote:
>> On Tuesday, February 7, 2023 at 10:24:47 PM UTC, Chris M. Thomasson wrote:
>> 
>>> No. Not threads. Processes. If a thread dies in a process it will take
>>> down the whole process.
>> 
>> I used the following function:
>> 
>>         https://man7.org/linux/man-pages/man3/pthread_mutexattr_setrobust.3.html
>> 
>> Here's an excerpt:
>> 
>>         "The robustness attribute specifies the behavior of the mutex when the owning thread dies without unlocking the mutex."
>
>If the owning thread dies, it means the process has died. robust mutexes 
>are for inter process sync.

The owning thread could exit (or be cancelled) without releasing the mutex
and without killing the process.

That said, I think the application should be designed such that robust
mutexes are not required;  Just because a robust mutex automatically
becomes unlocked - that doesn't prevent the state protected by the mutex
from being partially updated and hence buggy when the robust mutex was released.

If the dead thread had been in the middle of a linked list update, for
example, the list could end up with a loop or sans a bunch of entries.

[toc] | [prev] | [next] | [standalone]


#89036

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 16:06 -0800
Message-ID<trup27$3u658$1@dont-email.me>
In reply to#89035
On 2/7/2023 3:01 PM, Scott Lurndal wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>> On 2/7/2023 2:51 PM, Frederick Virchanza Gotham wrote:
>>> On Tuesday, February 7, 2023 at 10:24:47 PM UTC, Chris M. Thomasson wrote:
>>>
>>>> No. Not threads. Processes. If a thread dies in a process it will take
>>>> down the whole process.
>>>
>>> I used the following function:
>>>
>>>          https://man7.org/linux/man-pages/man3/pthread_mutexattr_setrobust.3.html
>>>
>>> Here's an excerpt:
>>>
>>>          "The robustness attribute specifies the behavior of the mutex when the owning thread dies without unlocking the mutex."
>>
>> If the owning thread dies, it means the process has died. robust mutexes
>> are for inter process sync.
> 
> The owning thread could exit (or be cancelled) without releasing the mutex
> and without killing the process.

Touche. Yeah, well, I was thinking of died as in seg fault without 
handling it. I have never had a use for the following:

https://linux.die.net/man/3/pthread_kill

Never had any use for pthread_cancel. I have only used robust mutexes 
for inter process communication. Using them for anything else is really 
strange to me. Threads just up and dying in a process is NOT kosher, in 
my humble opinion. However, a process dying is a different story.


> That said, I think the application should be designed such that robust
> mutexes are not required;  Just because a robust mutex automatically
> becomes unlocked - that doesn't prevent the state protected by the mutex
> from being partially updated and hence buggy when the robust mutex was released.
> 
> If the dead thread had been in the middle of a linked list update, for
> example, the list could end up with a loop or sans a bunch of entries.

Yup. Iirc, I had to do some tricky rollback operations back when I had 
to use them. A process dying had to be handled. I never had to rely on 
robust mutexes for intra process sync, only inter process.

[toc] | [prev] | [next] | [standalone]


#89030

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 14:28 -0800
Message-ID<trujb2$3tacv$2@dont-email.me>
In reply to#89028
On 2/7/2023 2:17 PM, Frederick Virchanza Gotham wrote:
> On Tuesday, February 7, 2023 at 8:22:37 PM UTC, Chris M. Thomasson wrote:
> 
>> It's horrible. Well, I had to deal with robust mutexes many moons ago.
> 
> 
> I've worked with robust mutexes in Linux; if the thread that acquired the lock dies, the lock is released.
> 
> However I'm trying to accommodate a situation in which a worker thread freezes -- the thread's still alive but it's caught in an infinite loop or a blocking function won't return.
> 
> In your previous post you said that the thread that unlocks a mutex must be that one had locked it ... well I'll use an atomic_flag like I described in the link I gave in my original post.

Huh? Are you creating a mutex or a binary semaphore? They are quite 
different.

[toc] | [prev] | [next] | [standalone]


#89012

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-07 12:23 +0200
Message-ID<trt8rr$3ibct$1@dont-email.me>
In reply to#89009
07.02.2023 11:25 Frederick Virchanza Gotham kirjutas:

> 
> A well-written program should work properly 99.9% of the time. 
So if our customer wants to analyze yet another batch of their 100,000 
microscope images, the program would randomly fail on 100 images. Not a 
great selling point.

[toc] | [prev] | [next] | [standalone]


#89033

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 14:53 -0800
Message-ID<trukoe$3tgjl$2@dont-email.me>
In reply to#89009
On 2/7/2023 1:25 AM, Frederick Virchanza Gotham wrote:
> 
> Since C++11, we already have "std::timed_mutex". It will try to acquire the lock for N seconds and fail, and you can handle the failure.
> 
> I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.
> 
> Last week I posted code here for a mutex that could be unlocked by a thread other than the one that locked it:
> 
>      https://groups.google.com/g/comp.lang.c++/c/HtUKQlNUI50
> 
> Similarly, I would use an "std::atomic_flag" as a mutex that can be unlocked by any thread.
> 
> A well-written program should work properly 99.9% of the time. There will always be unforeseen problems like a hardware failure, or a yet-to-be-discovered bug in a third-party library, or an operating system bug, or another process is set to a high priority with high CPU usage. This is why, when I'm finished writing a program that works very well, I try to add extra robustness to it in order to try make it recover gracefully from unforeseen errors. This means I might add in code to kill a thread that has become unresponsive. This is why I'm thinking I should write a 'timed_rescuable_mutex". In my code I would then use it with "std::unique_lock::try_lock_for".

You might be interested in cancellation points and pthread_cancel, but 
its a bitch and a half to use. asynchronous or deferred canceling. There 
are better ways. Start with the actual design. asynchronous 
cancelability should be avoided at all costs! deferred makes more sense, 
but still...

[toc] | [prev] | [next] | [standalone]


#89037

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-07 16:08 -0800
Message-ID<trup55$3u55d$1@dont-email.me>
In reply to#89009
On 2/7/2023 1:25 AM, Frederick Virchanza Gotham wrote:
> 
> Since C++11, we already have "std::timed_mutex". It will try to acquire the lock for N seconds and fail, and you can handle the failure.
> 
> I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.
[...]

My god, that is a bad idea.

[toc] | [prev] | [next] | [standalone]


#89041

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-08 10:37 -0800
Message-ID<429effe5-51d7-4e68-8b39-4679da12fee1n@googlegroups.com>
In reply to#89037
On Wednesday, February 8, 2023 at 12:08:20 AM UTC, Chris M. Thomasson wrote:
> > I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.
> [...] 
> 
> My god, that is a bad idea.

is it worse than the entire program locking up because one thread has frozen?

I'm proposing a solution that is less bad than the problem. Better to kill a frozen thread and discard its work, than have the entire program lock up.

[toc] | [prev] | [next] | [standalone]


#89042

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-08 20:52 +0200
Message-ID<ts0r1c$9qei$1@dont-email.me>
In reply to#89041
08.02.2023 20:37 Frederick Virchanza Gotham kirjutas:
> On Wednesday, February 8, 2023 at 12:08:20 AM UTC, Chris M. Thomasson wrote:
>>> I want to make another kind of timed mutex, called something like "std::timed_rescuable_mutex", but the difference will be that when you invoke "try_lock_for", it will wait for N seconds, and then if the mutex is still locked, it will kill the thread that locked it, and then unlock the mutex.
>> [...]
>>
>> My god, that is a bad idea.
> 
> is it worse than the entire program locking up because one thread has frozen?
> 
> I'm proposing a solution that is less bad than the problem. Better to kill a frozen thread and discard its work, than have the entire program lock up.

If your program has a bug so that a thread gets stuck, then find and fix 
the bug. Do not try to hide it or to work around it, it will just cause 
endless pain and in the end you still need to fix the bug.

Do not forget to create an automated test case to ensure the bug is gone 
and does not come back.

Ah, I know you won't listen to me anyway, but maybe others do.

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c++


csiph-web