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


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

Re: std::thread does not follow RAII principles

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c++
Subject Re: std::thread does not follow RAII principles
Date 2021-05-26 17:44 +0200
Organization Aioe.org NNTP Server
Message-ID <s8lqdp$16h$1@gioia.aioe.org> (permalink)
References <s8lcnq$p2a$1@gioia.aioe.org> <s8lmb5$7v8$1@dont-email.me>

Show all headers | View raw


On 5/26/2021 4:35 PM, Paavo Helde wrote:
> 26.05.2021 14:51 Juha Nieminen kirjutas:
>> I have noticed a rather odd, and somewhat annoying, design decision in
>> std::thread, which breaks RAII design.
> 
>>
>> the program will still crash, because std::thread does not like being
>> destroyed without being joined or detached, so it just terminates if 
>> that's
>> the case.
> 
> std::thread is based on boost::thread which automatically detaches in 
> the destructor. Alas, this would create a rampant thread which cannot be 
> joined any more.
> 
> I guess when it got standardized, they felt that such automatic detach 
> is no good, but did not dare to enforce automatic join either, by some 
> reason. So they chose to std::terminate() which is the worst of them all 
> IMO.
> 
> See https://isocpp.org/files/papers/p0206r0.html (Discussion about 
> std::thread and RAII).

Having the thread to detach on destruction is a poor choice, IMO.
On the other hand defaulting to join would lead to buggy programs likely 
to hang indefinitely instead of recovering or terminate. I can 
understand the committee's concerns in this sense.

We see more and more commonly (admittedly in mediocre programs) that 
unexpected default actions provided by "easy to use" languages bubble up 
to the level of user's experience because of software bugs - this 
reflects poorly on the perceived quality of the language itself.

However, I agree with the OP that this is inconsistent with RAII principles.

Thanks for the link, btw.

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


Thread

std::thread does not follow RAII principles Juha Nieminen <nospam@thanks.invalid> - 2021-05-26 11:51 +0000
  Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 14:04 +0200
    Re: std::thread does not follow RAII principles MrSpook__u_8@_4fk8arybb76.eu - 2021-05-26 15:25 +0000
      Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:50 +0200
  Re: std::thread does not follow RAII principles Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-26 17:35 +0300
    Re: std::thread does not follow RAII principles Manfred <noname@add.invalid> - 2021-05-26 17:44 +0200
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:52 +0200
      Re: std::thread does not follow RAII principles Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-26 21:41 +0300
  Re: std::thread does not follow RAII principles scott@slp53.sl.home (Scott Lurndal) - 2021-05-26 15:20 +0000
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:54 +0200
      Re: std::thread does not follow RAII principles MrSpook_c7o6A5w1@9wp2.net - 2021-05-26 16:28 +0000
        Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 19:04 +0200
  Re: std::thread does not follow RAII principles MrSpook_0crgrbi6@q0jfxa33c.biz - 2021-05-26 15:24 +0000
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 18:02 +0200

csiph-web