Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multithreading in static object construction

28 views
Skip to first unread message

Juha Nieminen

unread,
Jan 24, 2019, 12:15:15 PM1/24/19
to
Static objects, such as ones in namespaces (including nameless
namespaces) and static class members, are constructed before
main() is even called.

I just want to make sure: Is it ok to use std::thread and the
other multithreading standard utilities in the constructors of
these objects?

We can assume that no such object needs anything from another
object in another compilation unit.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Sam

unread,
Jan 24, 2019, 5:37:15 PM1/24/19
to
Juha Nieminen writes:

> I just want to make sure: Is it ok to use std::thread and the
> other multithreading standard utilities in the constructors of
> these objects?
>
> We can assume that no such object needs anything from another
> object in another compilation unit.

That's the only assumption that you must make. Otherwise, that's fine.

Chris M. Thomasson

unread,
Jan 24, 2019, 9:34:55 PM1/24/19
to
On 1/24/2019 9:15 AM, Juha Nieminen wrote:
> Static objects, such as ones in namespaces (including nameless
> namespaces) and static class members, are constructed before
> main() is even called.
>
> I just want to make sure: Is it ok to use std::thread and the
> other multithreading standard utilities in the constructors of
> these objects?

Just be careful. I have seen nightmares where people create a thread in
the ctor, that calls a member of the object currently being constructed!
The damn thread calls the member _before_ the object is fully
constructed. This IS BADDDDD! I have had nightmares about fixing some
others code before.
0 new messages