I think this may help you with what it is you are trying to achieve. Now the template class I'm showing you is written with use of the Windows Platform for locking threads, you can modify this class to work with other OS - Platforms as needed. It is just used as an illustration of how one could achieve the above semantics. This does compile, run and exits with a code of 0 for Visual Studio 2015 CE. This class does rely on the <Windows.h>
header file for the use of CRITICAL_SECTION
, EnterCriticalSection()
, LeaveCriticalSection()
, InitializeCriticalSection()
& DeleteCriticalSection()
. If there is an alternative to these with in other libraries such as the boost library this class can easily be written to achieve the same functionality. This class is designed to lock a user defined class object as volatile while working across multiple threads.
VolatileLocker.h
#ifndef VOLATILE_LOCKER_H
#define VOLATILE_LOCKER_H
#include <Windows.h>
template<typename T>
class VolatileLocker {
private:
T* m_pObject;
CRITICAL_SECTION* m_pCriticalSection;
public:
VolatileLocker( volatile T& objectToLock, CRITICAL_SECTION& criticalSection );
~VolatileLocker();
T* operator->();
private:
VolatileLocker( const VolatileLocker& c ); // Not Implemented
VolatileLocker& operator=( const VolatileLocker& c ); // Not Implemented
}; // VolatileLocker
#include "VolatileLocker.inl"
#endif // VOLATILE_LOCKER_H
VolatileLocker.inl
// ----------------------------------------------------------------------------
// VolatileLocker()
// Locks A Volatile Variable So That It Can Be Used Across Multiple Threads Safely
template<typename T>
VolatileLocker<T>::VolatileLocker( volatile T& objectToLock, CRITICAL_SECTION& criticalSection ) :
m_pObject( const_cast<T*>( &objectToLock ) ),
m_pCriticalSection( &criticalSection ) {
EnterCriticalSection( m_pCriticalSection );
} // VolatileLocker
// ----------------------------------------------------------------------------
// ~VolatileLocker()
template<typename T>
VolatileLocker<T>::~VolatileLocker() {
LeaveCriticalSection( m_pCriticalSection );
} // ~VolatileLocker
// ----------------------------------------------------------------------------
// operator->()
// Allow The Locked Object To Be Used Like A Pointer
template <typename T>
T* VolatileLocker<T>::operator->() {
return m_pObject;
} // operator->
VolatileLocker.cpp
#include "VolatileLocker.h"
Now here is the main running application that uses the templated volatile locker class and the use of the placement new operator.
#include <iostream>
#include "VolatileLocker.h"
static CRITICAL_SECTION s_criticalSection;
class SomeClass {
private:
int m_value;
public:
explicit SomeClass( int value ) : m_value( value ) {}
int getValue() const { return m_value; }
}; // SomeClass
int main() {
InitializeCriticalSection( &s_criticalSection ); // Initialize Our Static Critical Section
SomeClass localStackObject( 2 ); // Create A Local Variable On The Stack And Initialize It To Some Value
// Create A Pointer To That Class And Initialize It To Null.
SomeClass* pSomeClass = nullptr;
// Not Using Heap Here, Only Use Local Stack For Demonstration, So Just Get A Reference To The Stack Object
pSomeClass = &localStackObject;
// Here Is Our Pointer / Reference To Our Class As A Volatile Object
// Which Is Also Locked For Thread Safety Across Multiple Threads
// And We Can Access The Objects Fields (public variables, methods) via
// the VolatileLocker's overloaded ->() operator.
std::cout << VolatileLocker<SomeClass>( *pSomeClass, s_criticalSection )->getValue() << std::endl;
// Placement New Operator On Our Pointer To Our Object Using The Class's Constructor
new (pSomeClass) SomeClass( 4 );
// Again Using The Volatile Locker And Getting The New Value.
std::cout << VolatileLocker<SomeClass>( *pSomeClass, s_criticalSection )->getValue() << std::endl;
// Here Is The Interesting Part - Let's Check The Original Local Stack Object
std::cout << localStackObject.getValue() << std::endl;
// Cleaning Up Our Critical Section.
DeleteCriticalSection( &s_criticalSection );
return 0;
} // main
Output
2
4
4
NOTE:
Something to be aware of. The initial local stack variable itself is not volatile. If you try to declare the stack variable as volatile and used it directly as such:
volatile SomeClass localStackObject( 2 );
SomeClass* pSomeClass = nullptr;
pSomeClass = &localStackObject; // Invalid - volatile SomeClass* cannot be assigned to an entity of type SomeClass*
If you try to work around this by using the volatile local variable directly you can still use it with the VolatileLocker, but you won't be able to use the Placement New as this snippet shows:
std::cout << VolatileLocker<SomeClass>( localStackObject, s_criticalSection )->getValue() << std::endl; // Line Okay - Notice using object directly and no dereferencing.
// However when we get to this line of code here:
new (localStackObject) SomeClass( 4 ); // Does Not Compile. There Is No Instance Of Operator New To Match The Argument List
// To Fix That We Can Do This:
new ( const_cast<SomeClass*>( &localStackObject) ) SomeClass( 4 ); // This Will Compile
However to access any members using this design method you would then have to use the VolatileLocker to access the class's methods so the localStackObject can not be used directly .
// This Is Invalid:
std::cout << localStackObject.getValue() << std::endl;
// Use This Instead:
std::cout << VolatileLocker<SomeClass>( localStackObject, s_criticalSection )->getValue() << std::endl;
As an important reminder note this class was originally designed with the specific windows platform in mind, however, the concept of this template class can easily be written with cross-platform modularity in mind just by replacing the CRITICAL_SECTION with any available cross-platform equivalent functions.
Here is a reference answer for working with Linux / Unix based systems:
stackoverflow/multithreading/linux
Here is a reference answer for working with Mac / Apple based systems:
stackoverflow/multithreading/mac
Here are references to write cross-platform modularity equivalents:
- cppreference/thread
- cppreference/condition_variable
volatile
is relevant