CloudBuilder C++ SDK  v3.0.0
Making social games is easy !
Public Member Functions | List of all members
CotCHelpers::CProtectedVariable< T > Class Template Reference

#include <CotCHelpers.h>

Public Member Functions

 CProtectedVariable (T value)
 
T * LockVar ()
 
T * UnlockVar ()
 
void SignalOne ()
 
void SignalAll ()
 
bool Wait (int timeoutMilliseconds=0)
 

Detailed Description

template<class T>
class CotCHelpers::CProtectedVariable< T >

Represents a locked variable. It is used when a content has to be accessed mutually exclusively on multiple threads, and you want one of the threads to wait for a modification.

CProtectedVariable<int> countGuard(0);
void takeOneAndWaitUntilAvailable() {
int *count = countGuard.LockVar();
while (*count == 0) {
countGuard.Wait();
}
(*count) -= 1;
count = countGuard.UnlockVar();
}
void giveOne() {
int *count = countGuard.LockVar();
if (*count == 0) {
countGuard.SignalAll();
}
(*count) += 1;
count = countGuard.UnlockVar();
}

Member Function Documentation

◆ LockVar()

template<class T >
T* CotCHelpers::CProtectedVariable< T >::LockVar ( )
inline

Request access to the variable (once you get it, you will be the only thread at the time).

Returns
a pointer to the protected variable for raw access

◆ SignalAll()

template<class T >
void CotCHelpers::CProtectedVariable< T >::SignalAll ( )
inline

Must be called within a LockVar/UnlockVar block.

◆ SignalOne()

template<class T >
void CotCHelpers::CProtectedVariable< T >::SignalOne ( )
inline

Must be called within a LockVar/UnlockVar block.

◆ UnlockVar()

template<class T >
T* CotCHelpers::CProtectedVariable< T >::UnlockVar ( )
inline

Unlock the variable, allowing it to be modified by other threads.

Returns
a null pointer that we recommend assigning to prevent further modification

◆ Wait()

template<class T >
bool CotCHelpers::CProtectedVariable< T >::Wait ( int  timeoutMilliseconds = 0)
inline

Waits for a call to Signal*() from another thread. Must be called within a LockVar/UnlockVar block.

Returns
whether the condition has been signaled (false means a timeout or error)

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