Game Dev/Scrap

Synchronized block in C++

AKer 2008. 6. 9. 10:37
반응형
출처 : http://ricanet.com/new/view.php?id=blog/050807



ACriticalSection someCriticalSection;
synchronized(someCriticalSection)
{
    ...
}  

1. 생성과 해제를 통한 자원 획득, 해제
class AutoLock
{
public:
    AutoLock( ACriticalSection& cs ) : cs(cs)
    {
        cs.Lock();
        cont = true;
    }
    ~AutoLock()
    {
        cs.Unlock();
    }
private:
    ACriticalSection&  cs;
};
{
    AutoLock(someCriticalSection);
    ...
}

2. for문을 이용한 매크로
class AutoLock
{
public:
    AutoLock( ACriticalSection& cs ) : cs(cs)
    {
        cs.Lock();
        cont = true;
    }
    ~AutoLock()
    {
        cs.Unlock();
    }
    void P()
    {
        cont = false;
    }
    bool C()
    {
        return cont;
    }
private:
    ACriticalSection&  cs;
    bool cont;
};
#define synchronized(A)    for(AutoLock _syncVar(A); _syncVar.C(); _syncVar.P())


3. 2번 예제의 문제점 1 - break
for( ... )
{
    synchronized(someCriticalSection)
    {
        if( blahblah )
            break;
    }
    // here
}
// there


4. 2번 예제의 문제점2 - VC 6.0
//  May cause a problem in VC 6.0
synchronized(someCriticalSection)
{
    ...
}
synchronized(someCriticalSection)
{
    ...
}


5. if문을 이용한 매크로
class AutoLock
{
public:
    AutoLock( ACriticalSection& cs ) : cs(cs)
    {
        cs.Lock();
    }
    ~AutoLock()
    {
        cs.Unlock();
    }
    operator bool()
    {
        return true;
    }
private:
    ACriticalSection&  cs;
};
#define synchronized(A)        if( AutoLock _syncVar = A )
반응형

'Game Dev > Scrap' 카테고리의 다른 글

Starcraft 2 Effects & Techniques  (0) 2008.08.20
The Function Pointer Tutorial  (0) 2008.08.11
Resource Management Best Practices  (0) 2008.07.23
Device Lost Handling  (0) 2008.07.23