Microsoft C++ Synchronization Object

Microsoft C++ Synchronization Object (동기화객체)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686364(v=vs.85).aspx

http://thermidor.tistory.com/23

 

Critical Section
(뮤텍스와 비슷하나, 동일 프로세서 내에서만 사용 가능하다.)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85).aspx

-InitializeCriticalSection // 초기화

-DeleteCriticalSection // 삭제

-EnterCriticalSection // lock

-LeaveCriticalSectgion // unlock

 

Mutex
(뮤텍스 객체를 생성하여 사용하므로 Critical Section에 비해서 느리다. 최초에 Signaled 상태로 생성되어지며, WaitForSingleObject 대기함수를 호출함으로써 NonSignaled 상태가 된다. Critical Section 객체의 경우 다른 스레드에 의해 소유되면 EnterCriticalSection은 무한정 기다리게되는 반면-즉, 데드락이 가능- 뮤텍스 객체는 WaitForSingleObject를 사용하여 타임아웃이 가능하다.)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms684266(v=vs.85).aspx

-CreateMutuex // 뮤텍스 생성 (전역으로 하나만 생성)

-OpenMutex // 뮤텍스 열기

-ReleaseMutex // 뮤텍스 소유 해제

-CloseHandle // 뮤텍스 파괴

-WaitForSingleObject // 동기화 대기

-WaitForMultipleObject // 여러 개의 동기화 객체 대기

 

Semaphore
(뮤텍스가 하나의 shared resource을 lock/unlock하는데 비해, 세마포어는 일정 개수를 가지는 자원을 lock/unlock할 수 있다. )

https://msdn.microsoft.com/en-us/library/windows/desktop/ms685129(v=vs.85).aspx

-CreateSemaphore // 세마포어 생성 (최대 사용개수 지정)

-OpenSemaphore // 세마포어 열기

-ReleaseSemaphore // 세마포어 소유 해제

 

Event
(Critical Section, Mutex, Semaphore가 공유자원 lock/unlock에 사용하는데 비해, 이벤트는 스레드의 작업순서나 시기를 조정하기 위해 사용된다.)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682655(v=vs.85).aspx

-CreateEvent // 이벤트 생성

-OpenEvent // 이벤트 열기

-SetEvent // 사용자 임의로 신호상태와 비신호상태를 설정가능

-ResetEvent // 이벤트 리셋

Lock, mutex, semaphore… what’s the difference?

http://stackoverflow.com/questions/2332765/lock-mutex-semaphore-whats-the-difference

Critical Section= User object used for allowing the execution of just one active thread from many others within one process. The other non selected threads are put to sleep.

Mutex Semaphore (aka Mutex)= Kernel object used for allowing the execution of just one active thread from many others, among different processes. The other non selected threads are put to sleep. This object supports thread ownership, thread termination notification, recursion (multiple ‘acquire’ calls from same thread) and ‘priority inversion avoidance’.

Counting Semaphore (aka Semaphore)= Kernel object used for allowing the execution of a group of active threads from many others. The other non selected threads are put to sleep.

Spin-lock (aka Spinlock)= A lock which uses busy waiting. (The acquiring of the lock is made by xchg or similar atomic operations).

Lamport vs Vector Clock

4 processes (P1, P2, P3, P4) with events a,b,c,d,e,f,g,…

Multiprocess-Events

Lamport Clock Timestamps
https://en.wikipedia.org/wiki/Lamport_timestamps

For each process, p:
    initialize the timestamp, TS = 0;
    on each event, e:
        if e is receiving message m, 
            p.TS = max(m.TS, p.TS);
        p.TS++; 
        e.TS = p.TS;
        if e is sending message m, 
            m.TS = p.TS;

Lamport-Clock-Timestamp

Vector Clock Timestamps
https://en.wikipedia.org/wiki/Vector_clock

For M processes:
    initialize the timestamp, p.VT = (0,0,..,0);
    on each event, e:
        if e is receiving message m, 
            for i=1 to M, 
                p.VT[i] = max(m.VT[I], p.VT[I]);
        p.VT[self]++; 
        e.VT = p.VT;
        if e is sending message m, 
            m.VT = p.VT;

 Vector-Clock-Timestamps