IEG 4180 Tutorial 5 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

IEG 4180 Tutorial 5

Description:

Critical section can only synchronize threads within the same process ... Can be used for inter-process synchronization. e.g. Mutex, Semaphore, Socket, Timer, ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 12
Provided by: courseIe
Category:
Tags: ieg | critical | tutorial

less

Transcript and Presenter's Notes

Title: IEG 4180 Tutorial 5


1
IEG 4180 Tutorial 5
  • Prepared by Zero
  • (Note Content adopt from LiKins tutorial notes)

2
Threads SynchronizationIntroduction
  • Sharing data between threads may pose
    synchronization problems
  • Data corruption
  • Synchronize threads by
  • Critical Section
  • Kernel Objects
  • Mutex
  • Semaphore

3
Thread SynchronizationCritical Section
  • A region of code that can only be accessed by one
    thread only

4
Thread SynchronizationCritical Section An
Example
  • Critical section can only synchronize threads
    within the same process
  • Use kernel objects to synchronize between
    processes
  • A critical section object will not be released
    automatically if the holding thread is terminated
    before calling LeaveCriticalSection()

5
Thread SynchronizationKernel Objects
  • Kernel objects
  • Data structures maintained internally by the OS
    kernel
  • Can be used for inter-process synchronization
  • e.g. Mutex, Semaphore, Socket, Timer,
  • Difference between Mutex and Semaphore
  • Mutex max. counter value 1
  • At most one thread can own it
  • Semaphore max. counter value N
  • At most N threads can own it

6
Thread SynchronizationKernel Objects Object
State
  • The counter
  • Decrease by one when a thread claims it
  • Increase by one when a thread releases it
  • Always non-negative and not larger than the max.
    value
  • Object state
  • A kernel object is in signaled state when its
    counter is larger than zero, otherwise it is in
    non-signaled state
  • A thread can only claims signaled kernel objects

7
Thread SynchronizationCreating Mutex Semaphore
  • Set bInitialOwner to true to claims the object as
    soon as it is being created
  • lpName specify the name of Mutex / Semaphore,
    which identify the kernel object across
    difference processes
  • Semaphore
  • lMaximumCount specify the max. counter value of
    created semaphore
  • lInitialCount specify the initial counter value
    of created semaphore

8
Thread SynchronizationClaiming Mutex Semaphore
  • WaitForSingleObject
  • Block until hHandle becomes signaled
  • WaitForMultipleObjects
  • bWaitAll true
  • Block until all objects in lpHandles become
    signaled
  • bWaitAll false
  • Block until one object in lpHandles becomes
    signaled

9
Thread SynchronizationCalling Sequence
10
Thread SynchronizationMutex Semaphore -
Examples
11
Thread SynchronizationExamples
  • void main(void)
  • HANDLE handlesnumThreads
  • DWORD threadID
  • INT i
  • for (i0 iltnumThreads i)
  • // create the threads
  • handlesiCreateThread(0, 0, CountThread,
  • NULL, 0, threadID)
  • // wait for all threads to finish execution
  • WaitForMultipleObjects(numThreads, handles, TRUE,
    INFINITE)
  • cout ltlt "Global count " ltlt count ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com