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


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

Re: Move constructor in lock_guard safe?

From Paavo Helde <eesnimi@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: Move constructor in lock_guard safe?
Date 2022-08-15 09:38 +0300
Organization A noiseless patient Spider
Message-ID <tdcpl5$3h3oh$1@dont-email.me> (permalink)
References <tdaghg$jkn$1@news.nntp4.net> <tdao7b$34vec$1@dont-email.me> <tdbgou$386bu$1@dont-email.me>

Show all headers | View raw


14.08.2022 22:00 Chris M. Thomasson kirjutas:
> On 8/14/2022 5:01 AM, Paavo Helde wrote:
>> 14.08.2022 12:50 Marcel Mueller kirjutas:
>>> In following code fragment a thread object is detached from a class 
>>> instance. (last line)
>>>
>>> class X
>>> {
>>>    std::mutex Lock;
>>>    std::thread Worker;
>>>    std::condition_variable CV;
>>
>> These two are in the wrong order. The thread member must be the last 
>> one, otherwise it may start running and access CV before CV is even 
>> created.
> [...]
> 
> Please, try not to create threads in an objects ctor when the thread is 
> going to start working on the state of the object as soon as its up and 
> running. I have had to debug full blown nightmares from other peoples 
> code using that horror. Oh crap, what a freak show of race conditions. 
> Now, X::Worker could be run later in a so-called "X::run()" function. 
> Worker = std::thread(...) _after_ the ctor is guaranteed to be 100% 
> complete...
> 
> Launching a thread in a ctor of object A can and will start operating on 
> the state of object A before the ctor of A is finished...
> 
> Is that kosher? Puke!


It's perfectly safe as long as the thread is the last member in the 
class and the constructor body is empty. My constructor bodies often are.

By artificially delaying the start of the thread you just introduce 
extra state and complicate the program, for no good reason.

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


Thread

Move constructor in lock_guard safe? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-08-14 11:50 +0200
  Re: Move constructor in lock_guard safe? Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-14 15:01 +0300
    Re: Move constructor in lock_guard safe? Öö Tiib <ootiib@hot.ee> - 2022-08-14 06:01 -0700
    Re: Move constructor in lock_guard safe? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-14 12:00 -0700
      Re: Move constructor in lock_guard safe? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-08-15 00:08 +0200
        Re: Move constructor in lock_guard safe? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-14 16:22 -0700
      Re: Move constructor in lock_guard safe? Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-15 09:38 +0300
        Re: Move constructor in lock_guard safe? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-14 23:56 -0700
      Re: Move constructor in lock_guard safe? scott@slp53.sl.home (Scott Lurndal) - 2022-08-15 15:31 +0000
        Re: Move constructor in lock_guard safe? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-15 11:11 -0700

csiph-web