Listing 3: interlok.hpp C++ wrapper for interlocked variables
#ifndef INTERLOCK_HPP #define INTERLOCK_HPP #define STRICT #define WIN32_LEAN_AND_MEAN #if !defined(_INC_WINDOWS) && !defined(__WINDOWS_H) # include <windows.h> #endif class Interlock { public: // Possible results of operations enum Results { Negative=-1, Zero=0, Positive=1 }; // Initialise with starting value inline Interlock(LONG start=0); // Return current value inline operator const LONG() const; // Pre-increment value inline const Results operator ++(); // Pre-decrement value inline const Results operator --(); private: // Can't copy Interlocks! Interlock(const Interlock &); // Can't assign Interlocks! Interlock &operator =(const Interlock &); // Current value LONG value; }; // Interlock inline Interlock::Interlock(LONG start) : value(start) { } // Interlock::Interlock(start) inline Interlock::operator const LONG() const { return value; } // Interlock::operator LONG inline const Interlock::Results Interlock::operator ++() { LONG result = InterlockedIncrement(&value); if (result<0) return Negative; if (result>0) return Positive; return Zero; } // Interlock::operator ++ inline const Interlock::Results Interlock::operator --() { LONG result = InterlockedDecrement(&value); if (result<0) return Negative; if (result>0) return Positive; return Zero; } // Interlock::operator -- #endif // INTERLOCK_HPP //End of File