Java Monitor

Java Monitor (classic reader-writer problem) 1 writer & 2 readers

JavaMultithreadMonitor

 

The writer fills data until the buffer is full. If the buffer is full (condition variable), then waits. Otherwise (i.e., the buffer is available), it fills data in the buffer. After that, it signals.

The reader reads data until the buffer is empty. If the buffer is empty (condition variable), then wait. Otherwise (i.e., the buffer is available), it reads data. After that, it signals.

Shared Memory & Semaphore

NOTE: BinarySemaphore is the same as Mutex (for one shared resource).

1. SharedMemoryBinarySemaphoreWriter & SharedMemoryBinarySemaphoreReader
First, run SharedMemoryBinarySemaphoreWriter.exe (this only writes data on the shared memory segment.)

SharedMemoryBinarySemaphoreWriter

Then, run SharedMemoryBinarySemaphoreReader.exe (it only reads data from the shared memory segment.)

SharedMemoryBinarySemaphoreReader

2. SharedMemoryBinarySemaphoreMasterSlave (Both master/slaves write/reads data to/from the shared memory segment.)
First, run SharedMemoryBinarySemaphoreMasterSlave.exe –master
Then, run SharedMemoryBinarySemaphoreMasterSlave.exe

SharedMemoryBinarySemaphoreMasterSlave

 

NOTE: Semaphore allows a specific number of processes/threads to enter.

3. SharedMemoryGeneralSemaphoreMasterSlave (classic reader-writer problem)

The writer fills data until the buffer is full. It waits emptyBufferSemaphore (e–) to be available. If so, it fills data. After that, it signals fullBufferSemaphore(f++) ready.

The reader reads data until the buffer is empty. It waits fullBufferSemaphore(f–) to be available. If so, it reads data. After that, it signals emptyBufferSemaphore(e++) to be ready.

1 master is the writer (fills data the shared memory buffer).

2 slaves are the readers (read data from the shared memory buffer; but their reader index must be synchronized).
First, run SharedMemoryGeneralSemaphoreMasterSlave.exe –master
Next, run SharedMemoryGeneralSemaphoreMasterSlave.exe
Then, run SharedMemoryGeneralSemaphoreMasterSlave.exe

SharedMemoryGeneralSemaphoreMasterSlave

 

Shared Memory & Mutex

1. SharedMemoryMutexWriter & SharedMemoryMutexReader
First, run SharedMemoryMutexWriter.exe (this only writes data on the shared memory segment.)
SharedMemoryMutexWriter

Then, run SharedMemoryMutexReader.exe (it only reads data from the shared memory segment.)
SharedMemoryMutexReader

2. SharedMemoryMutexMasterSlave (Both master/slaves write/reads data to/from the shared memory segment.)
First, run SharedMemoryMutexMasterSlave.exe –master
Then, run SharedMemoryMutexMasterSlave.exe
SharedMemoryMutexMasterSlave

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).