Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stroika::Foundation::Execution::SharedStaticData< T > Class Template Reference

#include <SharedStaticData.h>

Public Member Functions

nonvirtual T & Get ()
 

Detailed Description

template<typename T>
class Stroika::Foundation::Execution::SharedStaticData< T >

SharedStaticData<T> is used to safely create a copy of static data shared among various users in a thread safe way, and where the shared data goes away when the last reference does, and where the shared data is lazy constructed.

This is very similar to have a single static variable of type T, except that instead of having T constructed at global execution time, and destroyed at global object destruction time, it happens when the first owner comes into existence and when the last owner goes out of existence.

For example - if the shared data object creates threads, it can be a problem having this destroyed in a static (file scope) lifetime.

Note
This is also similar to the
See also
ModuleInit<> template, except that this is intended to give the user tighter control over lifetime of the shared data.
Note
Why use this instead of member function returning reference to local static object? Only real difference here is that this 'shared static' object will be auto-deleted when the last reference to it is destroyed (as opposed to after we start exiting main for static data member)

This can be important, if, for example, the shared object contains Thread objects.

Example Usage (from HealthFrameWorksServer)
class AuditLogSink {
...
private:
struct SharedData_;
};
struct AuditLogSink::SharedData_ {
Execution::Thread::Ptr fCleanupThread;
SharedData_ ()
: fCleanupThread (Thread::New (&AuditLogSink::SimpleCleanupThread_))
{
fCleanupThread.SetThreadPriority (Thread::Priority::eLowest);
fCleanupThread.SetThreadName ("AuditTrailCleanupThread"sv);
fCleanupThread.Start ();
}
~SharedData_ ()
{
fCleanupThread.AbortAndWaitForDone ();
}
};
Thread::Ptr is a (unsynchronized) smart pointer referencing an internally synchronized std::thread ob...
Definition Thread.h:334
nonvirtual void SetThreadName(const Characters::String &threadName) const
Definition Thread.cpp:710
nonvirtual void AbortAndWaitForDone(Time::DurationSeconds timeout=Time::kInfinity) const
Abort () the thread, and then WaitForDone () - but if doesn't finish fast enough, send extra aborts (...
Definition Thread.inl:291
nonvirtual void SetThreadPriority(Priority priority=Priority::eNormal) const
Definition Thread.cpp:689

So then on the first AuditLogSink construction - we construct the cleanup thread, and on the last the thread is shutdown. If these objects are all created after main, this assures the thread startup/cleanup is all done after the start of main and before it completes.

Note
Since this object is in its 'default initialized' state with all zeros, it is safe to use before the start of main (), and doesn't require the complicated inter-depependency managment of the @ModuleInit code.

Definition at line 85 of file SharedStaticData.h.

Member Function Documentation

◆ Get()

template<typename T >
T & Stroika::Foundation::Execution::SharedStaticData< T >::Get ( )

Return value guaranteed lifetime at least as long as 'this' object.

Note - though THIS is fully threadsafe, use of the reference T& is only as threadsafe as T itself.

Definition at line 38 of file SharedStaticData.inl.


The documentation for this class was generated from the following files: