C# Thread object lifetime

Word “thread” could mean several things here: System.Threading.Thread object (created by new Thread()), CLR thread (managed thread), OS thread (un-managed thread). Thread object will be candidate for GC as soon as Start() method completes, because there are no more references to it. Managed thread will stay alive while doSomeLengthyOperation() runs. Quoting the article by James … Read more

Lifetime of lambda objects in relation to function pointer conversion

§5.1.2/6 says: The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has … Read more

Is circumventing a class’ constructor legal or does it result in undefined behaviour?

It is legal now, and retroactively since C++98! Indeed the C++ specification wording till C++20 was defining an object as (e.g. C++17 wording, [intro.object]): The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.1), by a new-expression (8.5.2.4), when implicitly changing the active … Read more

When to use PerThreadLifetimeManager?

The Per Thread Lifetime is a very dangerous lifestyle and in general you should not use it in your application, especially web applications. This lifestyle should be considered dangerous, because it is very hard to predict what the actual lifespan of a thread is. When you create and start a thread using new Thread().Start(), you’ll … Read more

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

Why would the behavior of std::memcpy itself be undefined when used with non-TriviallyCopyable objects? It’s not! However, once you copy the underlying bytes of one object of a non-trivially copyable type into another object of that type, the target object is not alive. We destroyed it by reusing its storage, and haven’t revitalized it by … Read more