starting thread in a ctor – my take on a common pattern

It’s common to start a thread within a ctor, as seen in many articles and projects.

Danger – the half-cooked object under-construction should not be exposed and accessible in the child thread. Often in practice it’s not disastrous but accessing a half-cooked object is a code smell.

I’d say it’s fine if the code is simple and you know for sure the thread.start() is at end of ctor and the child thread doesn’t do anything fancy. Make sure other developers don’t mess things up. explains that constructor is single-threaded by default and is designed to run in a single-threaded “context”. Don’t break that assumption.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s