Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79841
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Ian Collins <ian-news@hotmail.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: std::thread does not follow RAII principles |
| Date | Fri, 28 May 2021 16:50:48 +1200 |
| Lines | 110 |
| Message-ID | <ihbb58Ft2e6U2@mid.individual.net> (permalink) |
| References | <s8lcnq$p2a$1@gioia.aioe.org> <CGtrI.183233$gq3.41312@fx04.iad> <s8lquu$9o4$3@dont-email.me> <s8lsul$1fkg$1@gioia.aioe.org> <s8lv3o$9rp$2@dont-email.me> <s8nkl1$avc$1@gioia.aioe.org> <s8nn6h$3je$1@dont-email.me> <s8ns0q$1q7m$1@gioia.aioe.org> <s8nshp$72b$1@dont-email.me> <s8nudr$vpq$1@gioia.aioe.org> <s8nuut$ml4$1@dont-email.me> <s8ofbl$1an0$1@gioia.aioe.org> <s8ol4c$7ig$1@dont-email.me> <20210527190000.513390cffd366ff30deb7a13@cvine--nospam--.freeserve.co.uk> <d9f55950-a420-4063-a4ca-86a1cc8a6803n@googlegroups.com> <ihb4kdFt2e6U1@mid.individual.net> <d106201a-aab3-4964-a415-5cc4dc7e482en@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Trace | individual.net z/pjBmdCMB9zEOUuuYtSxwT12Gm+YypAc07ErcWUFG1foNC7VQ |
| Cancel-Lock | sha1:z9E0TmK+eJc4DcJW/Ix6Xlul3rs= |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 |
| In-Reply-To | <d106201a-aab3-4964-a415-5cc4dc7e482en@googlegroups.com> |
| Content-Language | en-US |
| Xref | csiph.com comp.lang.c++:79841 |
Show key headers only | View raw
On 28/05/2021 15:37, Öö Tiib wrote:
> On Friday, 28 May 2021 at 05:59:41 UTC+3, Ian Collins wrote:
>> On 28/05/2021 12:52, Öö Tiib wrote:
>>> On Thursday, 27 May 2021 at 21:00:13 UTC+3, Chris Vine wrote:
>>>>
>>>> One other thing POSIX provides and C++11 doesn't is thread cancellation.
>>>> Some programmers who don't have relevant experience think thread
>>>> cancellation is a bad idea because it allows arbitrary cancellation
>>>> which cannot be adequately controlled (the "combing your hair with a
>>>> fork" jibe). Whilst that is true with windows it absolutely isn't with
>>>> POSIX. What you do is use deferred cancellation (the default in POSIX)
>>>> and block cancellation as normal policy, and only enable cancellation
>>>> at defined points in the code which are able to deal with it (normally
>>>> where a wait is to occur). Done properly, thread cancellation is far
>>>> easier to use than exceptions, which can jump out of anywhere if you
>>>> include std::bad_alloc in the mix. C++11 threads don't deal with
>>>> cancellation and will never be able to do so because they require OS
>>>> support - in this case POSIX support.
>>>
>>> With that thread cancellation I'm always in doubt. Especially if discussed
>>> in context of RAII and destructor of thing like std::thread.
>>>
>>> One of major reasons making a thread is to turn synchronous work into
>>> asynchronous. For that we put it into thread so then synchronous work is
>>> going on in thread doing its blocking operations (potentially checking if
>>> the work has become obsolete and/or reporting progress between those)
>>> and once complete (or failed) handing the results back over to caller thread.
>>> That way the caller thread has all benefits of asynchronous works going on.
>>> It is fully responsive, so can take care of other responsibilities, entertain
>>> users, report progresses, mark ongoing works obsolete and/or put out the
>>> results (or failures) once something becomes complete.
>>>
>>> But what is that cancellation? It is like telling to worker thread to die in
>>> middle of unknown state potentially in kernel with unknown amount of
>>> locks taken, files open or mapped to virtual memory, that fragile external
>>> hardware device produced by random morons in middle of something
>>> and what not? std::terminate feels better and more honest than to cancel
>>> the thread ... at least our users will see that we screwed all up in major
>>> way. It all gets of course lost in examples because these talk about "foo()".
>> Cancellation is well defined in POSIX. Cancellation points are defined
>> by the standard and threads can manage their own cancellation type and
>> state,
>>
>> If you want to manage resources in a thread that may be cancelled, you
>> can use cleanup functions. I haven't tried it in Linux, but certainly
>> in Solaris, the C++ runtime will destroy objects if the creating thread
>> is cancelled. This is one of those annoying behaviours not covered by
>> either the C++ or POSIX standards...
>>
>> A quick check on Linux shows that yes, destructors are called when a
>> thread is cancelled.
>
> Very interesting ... but magical solutions make me even more worried.
> Is there some kind of secret exception thrown or some kind of alternative
> stack unwinding used or what? If secret exception then does
> catch(...) { mopup(); throw; } work or has it to be full RAII? If alternative
> stack unwinding then what it costs and do noexcept(true) functions
> in call stack compile still into that rainbow table because of it?
I believe (at lease on Solaris), the "magic" is the runtime using
pthread_cleanup_push/pthread_cleanup_pop to manage the destructor
calling, there's no need for exceptions. There's nothing to stop you
doing this by hand...
There a a number of behaviours which fall between two standards and we
have to rely on the quality of the implementation, this just happens to
be one.
A simple test case:
#include <pthread.h>
#include <iostream>
#include <unistd.h>
struct foo
{
foo() { std::cout << "constructor" << std::endl; }
~foo() { std::cout << "destructor" << std::endl; }
};
void*
thread(void*)
{
foo f;
while (true)
{
sleep(5);
}
return nullptr;
}
int
main()
{
pthread_t t;
pthread_create(&t, 0, thread, 0);
sleep(1);
pthread_cancel(t);
pthread_join(t, 0);
}
--
Ian.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: std::thread does not follow RAII principles MrSpook_ry@939_6htz773e0qeya.eu - 2021-05-27 08:18 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 11:02 +0200
Re: std::thread does not follow RAII principles MrSpook_b28s@jxgz6zklebr1.tv - 2021-05-27 10:24 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 12:33 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 12:40 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 12:55 +0200
Re: std::thread does not follow RAII principles MrSpook_d4c3Jb7l7@8dftupdzk09cop2.co.uk - 2021-05-27 11:05 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 13:14 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 13:18 +0200
Re: std::thread does not follow RAII principles MrSpook_b_x@ukpge.org - 2021-05-27 15:54 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-27 19:32 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-27 19:00 +0100
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-27 17:52 -0700
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 05:24 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 05:27 +0200
Re: std::thread does not follow RAII principles Ian Collins <ian-news@hotmail.com> - 2021-05-28 14:59 +1200
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-27 20:37 -0700
Re: std::thread does not follow RAII principles Ian Collins <ian-news@hotmail.com> - 2021-05-28 16:50 +1200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 06:53 +0200
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-27 23:07 -0700
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 08:26 +0200
Re: std::thread does not follow RAII principles Ian Collins <ian-news@hotmail.com> - 2021-05-28 21:01 +1200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 13:04 +0200
Re: std::thread does not follow RAII principles Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-05-28 17:06 +0000
Re: std::thread does not follow RAII principles scott@slp53.sl.home (Scott Lurndal) - 2021-05-28 17:51 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 20:00 +0200
Re: std::thread does not follow RAII principles Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-05-28 21:42 +0000
Re: std::thread does not follow RAII principles Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-05-28 17:03 +0000
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 03:55 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 12:02 +0100
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 13:15 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 12:40 +0100
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 13:55 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 13:57 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 14:29 +0100
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 15:48 +0200
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 16:16 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 15:45 +0100
Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-28 17:30 +0200
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 19:54 +0100
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-28 15:08 +0100
Re: std::thread does not follow RAII principles Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-05-28 17:08 +0000
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-29 01:42 -0700
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-29 13:05 +0100
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-29 13:44 +0100
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-29 06:34 -0700
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-29 14:48 +0100
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-29 07:59 -0700
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-29 18:15 +0100
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-29 19:03 -0700
Re: std::thread does not follow RAII principles Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2021-05-30 11:48 +0100
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-30 09:35 -0700
Re: std::thread does not follow RAII principles Öö Tiib <ootiib@hot.ee> - 2021-05-29 06:51 -0700
Re: std::thread does not follow RAII principles MrSpook_jx@zp238i0_j1.net - 2021-05-28 11:11 +0000
csiph-web