thread safe and reentrant


Will a singleton class be thread safe and rentrant if it is used in a multithreading program?

In genera, if a class has a static member, can that class be used for thread safe and erentrant in a multithreading probram?

Thank you.

Kind Regards,

You seem to be a bit confused about what it means to be thread-safe and reentrant.

Code is thread-safe if it can be executed concurrently and correctly.
A function is reentrant if it can be started from some thread while some other thread is already executing it. This means that it cannot us static data (data that keeps its state between calls to a function), only local data, it can't lock unique resources (such as the console), and it can't call non-reentrant functions.

Classes, being data, are not thread-safe simply because that is a concept related to code, not to data.
A singleton's methods are only reentrant if they don't write to the object's members. I think it could work even if they only read OR write, but not both.
A method of a class with static data can be reentrant if the method doesn't use that data.
Topic archived. No new replies allowed.