Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85920
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Move constructor in lock_guard safe? |
| Date | 2022-08-14 15:01 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <tdao7b$34vec$1@dont-email.me> (permalink) |
| References | <tdaghg$jkn$1@news.nntp4.net> |
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.
>
> // ...
>
> std::thread Dispose()
> { std::lock_guard<mutex> lock(Lock);
> State = DIE;
> CV.notify_one();
> return std::move(Worker); // <-- this line
> }
> };
>
> The worker thread will terminate asynchronously and call the class
> destructor as last action. But it is essential the the Worker variable
> is no longer joinable at this point.
>
> Is it guaranteed that the move constructor has been completed its work
> before the lock is released? Or may RVO or whatever prevent this?
AFAIK, all side effects from a return statement are guaranteed to be
complete before local scope variables, incl. the lock guards, are
destroyed. So this line by itself is technically fine.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next 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