Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- class CriticalSection
- {
- public:
- CriticalSection()
- {
- InitializeCriticalSection(&m_cs);
- }
- ~CriticalSection()
- {
- DeleteCriticalSection(&m_cs);
- }
- void enterCriticalSection()
- {
- EnterCriticalSection(&m_cs);
- }
- void leaveCriticalSection()
- {
- LeaveCriticalSection(&m_cs);
- }
- protected:
- private:
- CRITICAL_SECTION m_cs;
- };
- template<class T>
- class SharedMemory
- {
- public:
- SharedMemory() { m_empty = true;}
- ~SharedMemory() {}
- T getData(bool& validreturn)
- {
- m_cs.enterCriticalSection();
- if (m_empty)
- {
- m_cs.leaveCriticalSection();
- validreturn = false;
- return T();
- }
- //Data in the shared memory
- T data = m_data;
- m_empty = true;
- m_cs.leaveCriticalSection();
- validreturn = true;
- return data;
- }
- bool setData(const T& data)
- {
- m_cs.enterCriticalSection();
- if (!m_empty)
- {
- m_cs.leaveCriticalSection();
- return false;
- }
- //Shared memory is empty so fill it
- m_data = data;
- m_empty = false;
- m_cs.leaveCriticalSection();
- return true;
- }
- protected:
- private:
- T m_data;
- bool m_empty;
- CriticalSection m_cs;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement