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 2 of 3 — ← Prev page 1 [2] 3  Next page →


#89045

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-08 12:37 -0800
Message-ID<ts1179$aq23$2@dont-email.me>
In reply to#89042
On 2/8/2023 10:52 AM, Paavo Helde wrote:
> 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.

Big time.


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

Taking the time to model a synchronization scheme in Relacy is a good thing:

https://github.com/dvyukov/relacy


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

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


#89046

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-08 15:14 -0800
Message-ID<1f9a0c63-308d-46d7-bcf3-50786261babfn@googlegroups.com>
In reply to#89042
On Wednesday, February 8, 2023 at 6:52:43 PM UTC, Paavo Helde wrote:

> 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. 


There hasn't been a bug reported yet.


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


I'm trying to make my program more robust and resilient, and so even though it seems to work fine in the handful of hours I test it, I don't assume that it always works perfectly in the dozens of hours that other people use it. I haven't had a thread freeze up on me yet......... but if it does happen in the future, I'd like to be ready for it.

Of course thought I might make my program *less* reliable by adding in precautionary stuff.

First I'll write the code for 'timeout_mutex' and see how it looks.

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


#89049

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-08 17:33 -0800
Message-ID<bc519f3c-e4de-4f75-afac-b159bd65468bn@googlegroups.com>
In reply to#89046
On Wednesday, February 8, 2023 at 11:14:45 PM UTC, Frederick Virchanza Gotham wrote:

> First I'll write the code for 'timeout_mutex' and see how it looks.


Here's the beginnings:

        https://godbolt.org/z/6rj355G7c

Of course there are race conditions in the setting of 'id' and 't' but I'll spend more time on it.

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


#89050

FromÖö Tiib <ootiib@hot.ee>
Date2023-02-08 17:45 -0800
Message-ID<b8eb3868-3ad4-4faa-9a17-f35f787a7320n@googlegroups.com>
In reply to#89046
On Thursday, 9 February 2023 at 01:14:45 UTC+2, Frederick Virchanza Gotham wrote:
> On Wednesday, February 8, 2023 at 6:52:43 PM UTC, Paavo Helde wrote: 
> 
> > 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.
> 
> There hasn't been a bug reported yet.

If there is a bug then such tricks can only make its discovering harder.
Your program has diagnosed its own madness of being stuck. Instead
of alerting and asking for aid it tries to hide it and do brain surgery to
itself. The result of that is very unlikely to be correct.
 

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


#89059

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-09 09:23 +0100
Message-ID<ts2ahr$k6o5$2@dont-email.me>
In reply to#89046
On 09/02/2023 00:14, Frederick Virchanza Gotham wrote:
> On Wednesday, February 8, 2023 at 6:52:43 PM UTC, Paavo Helde wrote:
> 
>> 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.
> 
> 
> There hasn't been a bug reported yet.
> 
> 
>> Do not forget to create an automated test case to ensure the bug is
>> gone and does not come back.
> 
> 
> I'm trying to make my program more robust and resilient, and so even
> though it seems to work fine in the handful of hours I test it, I
> don't assume that it always works perfectly in the dozens of hours
> that other people use it. I haven't had a thread freeze up on me
> yet......... but if it does happen in the future, I'd like to be
> ready for it.
> 

Certainly bugs can happen in code - no developer gets things right /all/ 
the time.  Put your emphasis on making it easier to write correct code, 
harder to write incorrect code, and easier to find the bugs, rather than 
trying to add software methods to handle software errors.

> Of course thought I might make my program *less* reliable by adding
> in precautionary stuff.
> 

Exactly - it's good that you realise that.  Such check features are 
usually untestable, and they will almost certainly make things worse.

> First I'll write the code for 'timeout_mutex' and see how it looks.

A half dozen experienced developers have told you it is a terrible idea. 
  Consider taking the hint.

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


#89060

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-09 01:24 -0800
Message-ID<2df24b6d-dc63-4254-90c6-5fa31218db93n@googlegroups.com>
In reply to#89059
I do extreme testing on my programs. I compile them with "-fsanitize=address" and then deliberately try to get them to crash (for example send them characters strings when they're expecting numbers, or click a push button 20 times in 8 seconds, or delete the open file while they're reading from it, or pull out the USB cable while they're communicating).

But let's say that I write a program perfectly. Let's say that I've accounted for every possible corner case, and that my program will always do its job properly. Now let's say in one of the worker threads, I have:

int Thread_Entry_Point(void *const arg)
{
    intptr_t const f = reinterpret_cast<intptr_t>(arg);  // file descriptor

    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 10000;

    int const rv = select(f, nullptr, nullptr, nullptr, &timeout);

    if (rv == -1)
       perror("select"); /* an error accured */
   else if(rv == 0)
      printf("timeout"); /* a timeout occured */
  else
    read(f, global_buf, sizeof global_buf); /* there was data to read */
}

Here's what it says in the Linux programmer's manual about the 'select' function:

             "The timeout argument is a timeval structure
              that specifies the interval that select() should block
              waiting for a file descriptor to become ready.  The call
              will block until either:
              • a file descriptor becomes ready;
              • the call is interrupted by a signal handler; or
              • the timeout expires."

Now let's say that the 'select' function, around 1 time in 650,000, never returns. Let's say the user plugs a USB stick into their laptop just as 'select' is called, or there's a CPU spike at that moment, or just some other unforeseen event that causes 'select' to block forever. I'm trying to account for circumstances such as these -- stuff like operating system issues. Computers aren't as perfect as some people think they are, I mean if you copy one terabyte of data from one place to another, you'll have at least 1 bit wrong.

If I write code for a worker thread that never ever ever should take more than 5 seconds to do its job, then killing it at 10 seconds, discarding its data and releasing its locks is better than allowing it to keep the lock and keep the entire program frozen.

On Thursday, February 9, 2023 at 8:23:38 AM UTC, David Brown wrote:
>
> A half dozen experienced developers have told you it is a terrible idea. 
> Consider taking the hint.

I do take advice and listen to other people's ideas, but I'm also aware of my own creativity. There's stuff I never would have pulled off if I'd acquiesced to everyone else -- the 'fat binary' for Windows and Linux being one example.

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


#89061

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-09 02:02 -0800
Message-ID<3e34a03f-f6d2-4aad-8f7a-c3ca20a87b26n@googlegroups.com>
In reply to#89060
On Thursday, February 9, 2023 at 9:24:36 AM UTC, Frederick Virchanza Gotham wrote:
>
> If I write code for a worker thread that never ever ever should take more than 5 seconds to do its job,
> then killing it at 10 seconds, discarding its data and releasing its locks is better than allowing it to keep
> the lock and keep the entire program frozen.

I did a little more work on it. You can see it on GodBolt here:

        https://godbolt.org/z/8G6s6Y3dx

and also I've copy-pasted it:

#include <cassert>             // assert
#include <cstdint>             // uint_fast32_t
#include <atomic>              // atomic_flag, atomic<T>
#include <mutex>               // mutex, condition_variable
#include <chrono>              // steady_clock, milliseconds, years
#include <condition_variable>  // condition_variable

#include <pthread.h>           // pthread_t, pthread_equal, pthread_self
#include <signal.h>            // pthread_kill

class timeout_mutex {

    // Flag to be used as the main lock (like a mutex)
    std::atomic_flag flag = ATOMIC_FLAG_INIT;

    // Combination of mutex and condition_variable
    // for waiting for a specific time interval
    std::mutex mtx_for_cv{};
    std::condition_variable cv{};

    // Information about the currently-held lock
    std::atomic<pthread_t> id{};
    std::atomic<std::chrono::time_point<std::chrono::steady_clock>> t =
        std::chrono::steady_clock::now() + std::chrono::years(65535u);

    // A flag to be used as a lock to ensure that two threads
    // don't simultaneously try to kill another thread
    std::atomic_flag flag_for_killing = ATOMIC_FLAG_INIT;

public:
    void lock_for_max(std::uint_fast32_t const millis) noexcept(false)
    {
        for (;;)
        {
            using namespace std::chrono;

            if ( false == flag.test_and_set() )  // Try to acquire main lock
            {
                id = pthread_self();
                t = steady_clock::now() + milliseconds(millis);
                return;
            }

            // If control reaches here, we failed to acquire the lock
            // So now we wait until one of two things happen:
            //   1) The lock becomes available
            //   2) The timeout expires

            { // Braces to ensure reduced scope for objects within
                std::unique_lock<std::mutex> mylock(mtx_for_cv);
                cv.wait_until(mylock,t.load());  // might get spurious wake-up here
            }

            if ( t.load() >= steady_clock::now() ) continue;  // No killing before timeout expired
            if ( false == flag.test()            ) continue;  // No killing if lock has become available

            // If control reaches here, we should kill the thread,
            // however two threads might simultaneously reach this
            // point, so we need another atomic_flag as a lock
            if ( false == flag_for_killing.test_and_set() )
            {   
                // One last check that main lock hasn't been released
                if ( flag.test() )
                {
                    pthread_kill(id,SIGKILL);  // or maybe pthread_cancel ?
                    // maybe put a delay here and check that the thread is dead
                    flag.clear();
                }

                flag_for_killing.clear();             
            }
        }
    }

    void unlock(void) noexcept
    {
        // The calling thread might be in the middle
        // of being killed right now, so acquire
        // the 'killing lock' before proceeding
        if ( flag_for_killing.test_and_set() ) return;

        assert( pthread_equal(pthread_self(),id) ); // Ensure current thread is the one that locked it
        assert( flag.test()                      ); // Ensure lock is still locked

        using namespace std::chrono;
        t = steady_clock::now() + years(65535u);
        id = {};
        flag_for_killing.clear();
        flag.clear();
        cv.notify_all();
    }
};

int main(void)
{

}

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


#89077

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-09 15:10 -0800
Message-ID<ts3uh2$q5ck$3@dont-email.me>
In reply to#89061
On 2/9/2023 2:02 AM, Frederick Virchanza Gotham wrote:
> On Thursday, February 9, 2023 at 9:24:36 AM UTC, Frederick Virchanza Gotham wrote:
>>
>> If I write code for a worker thread that never ever ever should take more than 5 seconds to do its job,
>> then killing it at 10 seconds, discarding its data and releasing its locks is better than allowing it to keep
>> the lock and keep the entire program frozen.
[...]

Now, are you trying to deal with real time sync? Look up wait-free sync.

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


#89078

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-10 08:06 -0800
Message-ID<bd9bcc18-80f4-428d-9564-5d3445974a89n@googlegroups.com>
In reply to#89077
Okay I'm starting to think that a process might not be salvageable
after any of its threads is killed or cancelled.

So instead maybe I could program a mutex watchdog system something
like as follows:
(1) Periodically check that all the worker threads are still alive.
(2) If one of the watchdog timers times out, do the following:
(2.a) Take a photograph of the GUI window on the screen, i.e. take
          a screenshot of it and save it as a bitmap.
(2.b) Save the values of all the widgets to a file (e.g. the text in
          text boxes, whether a tick box was ticked, the position
          of a slider).
(3) Start a new process that does the following:
(3.a) Displays the aforementioned bitmap on the screen over the real dialog
(3.b) Kills the main program
(3.c) Restarts the main program and loads the widget values from the file
(3.d) Removes the fake bitmap off the screen

The idea here is that the main program would be killed and restarted
without so much as a flicker on the screen.

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


#89079

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-10 17:30 +0100
Message-ID<ts5rei$13alh$1@dont-email.me>
In reply to#89078
On 10/02/2023 17:06, Frederick Virchanza Gotham wrote:
> 
> Okay I'm starting to think that a process might not be salvageable
> after any of its threads is killed or cancelled.

Progress!

> 
> So instead maybe I could program a mutex watchdog system something
> like as follows:
> (1) Periodically check that all the worker threads are still alive.
> (2) If one of the watchdog timers times out, do the following:
> (2.a) Take a photograph of the GUI window on the screen, i.e. take
>            a screenshot of it and save it as a bitmap.
> (2.b) Save the values of all the widgets to a file (e.g. the text in
>            text boxes, whether a tick box was ticked, the position
>            of a slider).
> (3) Start a new process that does the following:
> (3.a) Displays the aforementioned bitmap on the screen over the real dialog
> (3.b) Kills the main program
> (3.c) Restarts the main program and loads the widget values from the file
> (3.d) Removes the fake bitmap off the screen
> 
> The idea here is that the main program would be killed and restarted
> without so much as a flicker on the screen.
> 

You are over-thinking this all.

Make a supervisor process that monitors the working process, and kills 
then restarts it if necessary.  That's fine, and a commonly used 
strategy for high availability systems.

Having a method of tracking the existing settings or configuration, then 
replicating them on restart is perhaps useful, but perhaps worse than 
useless.  The process hung and was killed because of a software error - 
replicating the state could lead to exactly the same fault.  (This 
effect can be a PITA with web browsers that crash due to problems with a 
web page, then try to restore the session on restart and crash again.)

Faffing around with bitmaps and screenshots in order to avoid flicker 
is, however, utterly ridiculous.  If flicker is annoying users, then 
find and fix the software bug that is causing the hang - don't waste 
effort on a complicating system to try to hide the problem.  You'll just 
introduce even more bugs.


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


#89080

Fromscott@slp53.sl.home (Scott Lurndal)
Date2023-02-10 17:02 +0000
Message-ID<1NuFL.499058$Tcw8.104966@fx10.iad>
In reply to#89079
David Brown <david.brown@hesbynett.no> writes:
>On 10/02/2023 17:06, Frederick Virchanza Gotham wrote:
>> 
>> Okay I'm starting to think that a process might not be salvageable
>> after any of its threads is killed or cancelled.
>
>Progress!
>
>> 
>> So instead maybe I could program a mutex watchdog system something
>> like as follows:
>> (1) Periodically check that all the worker threads are still alive.
>> (2) If one of the watchdog timers times out, do the following:
>> (2.a) Take a photograph of the GUI window on the screen, i.e. take
>>            a screenshot of it and save it as a bitmap.
>> (2.b) Save the values of all the widgets to a file (e.g. the text in
>>            text boxes, whether a tick box was ticked, the position
>>            of a slider).
>> (3) Start a new process that does the following:
>> (3.a) Displays the aforementioned bitmap on the screen over the real dialog
>> (3.b) Kills the main program
>> (3.c) Restarts the main program and loads the widget values from the file
>> (3.d) Removes the fake bitmap off the screen
>> 
>> The idea here is that the main program would be killed and restarted
>> without so much as a flicker on the screen.
>> 
>
>You are over-thinking this all.
>
>Make a supervisor process that monitors the working process, and kills 
>then restarts it if necessary.  That's fine, and a commonly used 
>strategy for high availability systems.

Indeed.  Such an approach is less viable for the interactive GUI
program that Frederick is proposing, which may require that he
rethink the application and perhaps  disaggregate the worker
parts of the application from the display management elements.

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


#89086

FromRichard Damon <Richard@Damon-Family.org>
Date2023-02-10 20:02 -0500
Message-ID<OOBFL.1114566$GNG9.379808@fx18.iad>
In reply to#89080
On 2/10/23 12:02 PM, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 10/02/2023 17:06, Frederick Virchanza Gotham wrote:
>>>
>>> Okay I'm starting to think that a process might not be salvageable
>>> after any of its threads is killed or cancelled.
>>
>> Progress!
>>
>>>
>>> So instead maybe I could program a mutex watchdog system something
>>> like as follows:
>>> (1) Periodically check that all the worker threads are still alive.
>>> (2) If one of the watchdog timers times out, do the following:
>>> (2.a) Take a photograph of the GUI window on the screen, i.e. take
>>>             a screenshot of it and save it as a bitmap.
>>> (2.b) Save the values of all the widgets to a file (e.g. the text in
>>>             text boxes, whether a tick box was ticked, the position
>>>             of a slider).
>>> (3) Start a new process that does the following:
>>> (3.a) Displays the aforementioned bitmap on the screen over the real dialog
>>> (3.b) Kills the main program
>>> (3.c) Restarts the main program and loads the widget values from the file
>>> (3.d) Removes the fake bitmap off the screen
>>>
>>> The idea here is that the main program would be killed and restarted
>>> without so much as a flicker on the screen.
>>>
>>
>> You are over-thinking this all.
>>
>> Make a supervisor process that monitors the working process, and kills
>> then restarts it if necessary.  That's fine, and a commonly used
>> strategy for high availability systems.
> 
> Indeed.  Such an approach is less viable for the interactive GUI
> program that Frederick is proposing, which may require that he
> rethink the application and perhaps  disaggregate the worker
> parts of the application from the display management elements.
> 

I would think the simple answer is make the GUI process as bullet proof 
as possible and not need to do things that need these Mutexes, but spin 
up killable processes to do the work which sends answers back on 
something like a pipe to the GUI.

If a computation process hangs, you can then just kill it and restart, 
maybe letting the user know things are running slow.

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


#89153

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-17 12:58 -0800
Message-ID<tsoppe$3nl1r$1@dont-email.me>
In reply to#89086
On 2/10/2023 5:02 PM, Richard Damon wrote:
> On 2/10/23 12:02 PM, Scott Lurndal wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 10/02/2023 17:06, Frederick Virchanza Gotham wrote:
>>>>
>>>> Okay I'm starting to think that a process might not be salvageable
>>>> after any of its threads is killed or cancelled.
>>>
>>> Progress!
>>>
>>>>
>>>> So instead maybe I could program a mutex watchdog system something
>>>> like as follows:
>>>> (1) Periodically check that all the worker threads are still alive.
>>>> (2) If one of the watchdog timers times out, do the following:
>>>> (2.a) Take a photograph of the GUI window on the screen, i.e. take
>>>>             a screenshot of it and save it as a bitmap.
>>>> (2.b) Save the values of all the widgets to a file (e.g. the text in
>>>>             text boxes, whether a tick box was ticked, the position
>>>>             of a slider).
>>>> (3) Start a new process that does the following:
>>>> (3.a) Displays the aforementioned bitmap on the screen over the real 
>>>> dialog
>>>> (3.b) Kills the main program
>>>> (3.c) Restarts the main program and loads the widget values from the 
>>>> file
>>>> (3.d) Removes the fake bitmap off the screen
>>>>
>>>> The idea here is that the main program would be killed and restarted
>>>> without so much as a flicker on the screen.
>>>>
>>>
>>> You are over-thinking this all.
>>>
>>> Make a supervisor process that monitors the working process, and kills
>>> then restarts it if necessary.  That's fine, and a commonly used
>>> strategy for high availability systems.
>>
>> Indeed.  Such an approach is less viable for the interactive GUI
>> program that Frederick is proposing, which may require that he
>> rethink the application and perhaps  disaggregate the worker
>> parts of the application from the display management elements.
>>
> 
> I would think the simple answer is make the GUI process as bullet proof 
> as possible and not need to do things that need these Mutexes, but spin 
> up killable processes to do the work which sends answers back on 
> something like a pipe to the GUI.
> 
> If a computation process hangs, you can then just kill it and restart, 
> maybe letting the user know things are running slow.

Humm... I think there is a timer in windows that might "stop" a shader 
program after some time...

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


#89081

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-10 20:57 +0200
Message-ID<ts643e$14dr0$1@dont-email.me>
In reply to#89078
10.02.2023 18:06 Frederick Virchanza Gotham kirjutas:
> 
> Okay I'm starting to think that a process might not be salvageable
> after any of its threads is killed or cancelled.
> 
> So instead maybe I could program a mutex watchdog system something
> like as follows:
> (1) Periodically check that all the worker threads are still alive.

I'm not sure why you are fixated on thread deadlocks. Why don't you 
worry about NULL dereferences or other violations which will typically 
kill the process on spot?

> (2) If one of the watchdog timers times out, do the following:
> (2.a) Take a photograph of the GUI window on the screen, i.e. take
>            a screenshot of it and save it as a bitmap. > (2.b) Save the values of all the widgets to a file (e.g. the text in
>            text boxes, whether a tick box was ticked, the position
>            of a slider).

It would be much easier and more robust (and would also work in case of 
crash) to save settings in file or in registry as soon as they are changed.

> (3) Start a new process that does the following:
> (3.a) Displays the aforementioned bitmap on the screen over the real dialog
> (3.b) Kills the main program
> (3.c) Restarts the main program and loads the widget values from the file
> (3.d) Removes the fake bitmap off the screen
> 
> The idea here is that the main program would be killed and restarted
> without so much as a flicker on the screen.

I'm just curious - what is the intended purpose? You don't really expect 
the user is so dumb that they will not notice when your buggy program 
freezes or misbehaves, but yet will somehow notice some flicker on the 
screen?

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


#89062

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-09 04:02 -0800
Message-ID<ts2nbs$lpqv$1@dont-email.me>
In reply to#89046
On 2/8/2023 3:14 PM, Frederick Virchanza Gotham wrote:
> On Wednesday, February 8, 2023 at 6:52:43 PM UTC, Paavo Helde wrote:
> 
>> 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.
> 
> 
> There hasn't been a bug reported yet.
[...]

The plane went down... In this highly contrived scenario, the people who 
died can talk to each other after the died! Bear with me...

Hey man, "There hasn't been a bug reported yet."

Hummm....

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


#89064

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-09 04:16 -0800
Message-ID<b1699c11-597d-4640-9591-cf4aa0a40577n@googlegroups.com>
In reply to#89062
On Thursday, February 9, 2023 at 12:02:20 PM UTC, Chris M. Thomasson wrote:
>
> The plane went down... In this highly contrived scenario, the people who 
> died can talk to each other after the died! Bear with me... 
> 
> Hey man, "There hasn't been a bug reported yet." 
> 
> Hummm....


I'm in a theatre group so I can appreciate your input. But sometimes I want less poetry and more fact.

I posted code earlier today showing the beginnings of my "timeout_mutex". If you want to point out a specific weakness, please do so.

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


#89066

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-09 04:28 -0800
Message-ID<ts2ote$lpqs$2@dont-email.me>
In reply to#89064
On 2/9/2023 4:16 AM, Frederick Virchanza Gotham wrote:
> On Thursday, February 9, 2023 at 12:02:20 PM UTC, Chris M. Thomasson wrote:
>>
>> The plane went down... In this highly contrived scenario, the people who
>> died can talk to each other after the died! Bear with me...
>>
>> Hey man, "There hasn't been a bug reported yet."
>>
>> Hummm....
> 
> 
> I'm in a theatre group so I can appreciate your input. But sometimes I want less poetry and more fact.
> 
> I posted code earlier today showing the beginnings of my "timeout_mutex". If you want to point out a specific weakness, please do so.

Model it in Relacy.

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


#89067

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-09 14:36 +0200
Message-ID<ts2pd5$lvi7$1@dont-email.me>
In reply to#89064
09.02.2023 14:16 Frederick Virchanza Gotham kirjutas:
> On Thursday, February 9, 2023 at 12:02:20 PM UTC, Chris M. Thomasson wrote:
>>
>> The plane went down... In this highly contrived scenario, the people who
>> died can talk to each other after the died! Bear with me...
>>
>> Hey man, "There hasn't been a bug reported yet."
>>
>> Hummm....
> 
> 
> I'm in a theatre group so I can appreciate your input. But sometimes I want less poetry and more fact.
> 
> I posted code earlier today showing the beginnings of my "timeout_mutex". If you want to point out a specific weakness, please do so.

You used pthread_kill() there. It will kill the thread, but with these 
specific weaknesses, and more:

    - any dynamically allocated memory which was going to be released by 
this thread, is leaked.

    - any mutexes locked by the thread (other than the single one you 
bothered to unlock) remain locked and cannot be unlocked, potentially 
causing further deadlocks.

    - any files and other external resources opened by the thread remain 
opened and may cause access denials, or consume unneeded resources.

    - any content written to output files and other IO might remain 
inconsistent or in non-flushed buffers.

    - any changes which the thread made under the now-unlocked mutex 
might have remained uncompleted and leave the data structure in an 
invalid state. As the mutex is now unlocked forcefully, there is nothing 
to prevent other threads to access invalid data.

There are probably more. For me personally, a single such "specific 
weakness" is more than enough to abandon the whole idea.

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


#89070

FromFrederick Virchanza Gotham <cauldwell.thomas@gmail.com>
Date2023-02-09 08:34 -0800
Message-ID<beb0366b-3faa-4de1-a688-2d1d17d69cb8n@googlegroups.com>
In reply to#89067
On Thursday, February 9, 2023 at 12:37:07 PM UTC, Paavo Helde wrote:
>
> You used pthread_kill() there.


I'll also take a look at pthread_cancel.


> It will kill the thread, but with these 
> specific weaknesses, and more: 
> 
> - any dynamically allocated memory which was going to be released by 
> this thread, is leaked.


I could overload 'operator new' and maintain a list of memory allocations made by "std::this_thread::get_id()". So then when I kill the thread, I can free the thread's memory.

 
> - any mutexes locked by the thread (other than the single one you 
> bothered to unlock) remain locked and cannot be unlocked, potentially 
> causing further deadlocks.


There's less than 5 locks in the entire program so this won't be difficult to make sure of.

 
> - any files and other external resources opened by the thread remain 
> opened and may cause access denials, or consume unneeded resources. 


Again I can keep an eye on device opened like COM ports, and any open files, and any global resource.

 
> - any content written to output files and other IO might remain 
> inconsistent or in non-flushed buffers.


This will be the case anyway if the thread freezes.


> - any changes which the thread made under the now-unlocked mutex 
> might have remained uncompleted and leave the data structure in an 
> invalid state.
> As the mutex is now unlocked forcefully, there is nothing 
> to prevent other threads to access invalid data. 


I would make sure to discard all of the thread's work after killing it so that other threads don't access garbage. I would do this before releasing the lock.


> There are probably more. For me personally, a single such "specific 
> weakness" is more than enough to abandon the whole idea.


I think all of these perils you've mentioned are better than the application just locking up, and MS-Windows showing a progress bar saying "We're contacting Microsoft to tell them this program crashed".

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


#89071

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-09 20:25 +0100
Message-ID<ts3hbm$oq47$1@dont-email.me>
In reply to#89070
On 09/02/2023 17:34, Frederick Virchanza Gotham wrote:
> On Thursday, February 9, 2023 at 12:37:07 PM UTC, Paavo Helde wrote:
>> 
>> You used pthread_kill() there.
> 
> 
> I'll also take a look at pthread_cancel.
> 
> 
>> It will kill the thread, but with these specific weaknesses, and
>> more:
>> 
>> - any dynamically allocated memory which was going to be released
>> by this thread, is leaked.
> 
> 
> I could overload 'operator new' and maintain a list of memory
> allocations made by "std::this_thread::get_id()". So then when I kill
> the thread, I can free the thread's memory.
> 
> 

That is not sufficient.  When you are killing a thread from the outside, 
it could be killed at any time.  That can include while it is in the 
middle of an allocation, which may leave the various memory-tracking 
structures in an inconsistent state.  The same applies to all kinds of 
resources and data structures.

The OS will clear up most resources if a process is killed.  But if you 
just kill a thread, you have no way to be sure of re-establishing 
consistency and a working process.  You can get some level of assurance 
by implementing your own memory management system (at lot more than just 
overriding new), and other resource tracking - but with all the effort 
involved, and the overheads, you would be far better off using multiple 
processes instead of multiple threads.

Note that no matter how fantastic your test procedures are, you will 
probably not find inconsistency bugs in testing.  But they can still 
happen - traditionally, they occur after you have deployed thousands of 
systems or when you are demonstrating the program for your biggest 
potential customer.  Testing can only ever prove the /existence/ of 
bugs, never their /absence/ - for that, you need to design the software 
so that they can't happen.

Basically, you are trying to do your robustness here at the wrong level. 
  Threads live and die together.  If you need a "watchdog" system to 
monitor and kill a misbehaving task, kill the process, not a thread.

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


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

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


csiph-web