Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85939
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Move constructor in lock_guard safe? |
| Date | 2022-08-15 11:11 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <tde299$3o1s0$1@dont-email.me> (permalink) |
| References | <tdaghg$jkn$1@news.nntp4.net> <tdao7b$34vec$1@dont-email.me> <tdbgou$386bu$1@dont-email.me> <1FtKK.131032$dh2.83372@fx46.iad> |
On 8/15/2022 8:31 AM, Scott Lurndal wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>> 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...
>
> That's very easy to handle. The run function waits on a condition
> variable and the code that creates the object which contains the thread
> signals the condition variable after the object creation is complete.
>
> We do that all the time.
[ snip code ]
That will work as well. :^)
Still, my experience with having to debug race conditions in ctors might
of damaged me in a way... I always try not to launch threads in a ctor
that operate on the object being constructed. Damaged goods... ;^o
Actually, some of the worst bugs have had to correct is when an object
derives from a "thread object" that starts a thread that ends up calling
a virtual function into the object. Is that ctor of the derived object
done yet? Even though I was sometimes paid to do it, it left mental
scars in my brain. Yikes!
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll 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