Multithread API - PowerPoint PPT Presentation

About This Presentation
Title:

Multithread API

Description:

Title: Algebraic Topology and Distributed Computing Author: Maurice Herlihy Last modified by: Adam Piotrowski Created Date: 5/12/1999 1:47:53 PM Document presentation ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 80
Provided by: Maurice55
Category:

less

Transcript and Presenter's Notes

Title: Multithread API


1
Multithread APIs
  • Adam Piotrowski
  • Grzegorz Jablonski

Lecture IV
2
Synchronisation
  • Mutexes
  • Semaphores
  • Condition Variables

3
Mutex
  • The mutual exclusion lock is the simplest and
    most primitive synchronization variable. It
    provides a single, absolute owner for the section
    of code (thus a critical section) that it
    brackets between the calls to pthread_mutex_lock()
  • and pthread_mutex_unlock(). The first thread that
    locks the mutex gets ownership, and any
    subsequent attempts to lock it will fail, causing
    the calling thread to go to sleep.

4
Mutex initialisation
  • NAME
  • pthread_mutex_init, pthread_mutex_destroy -
    initializes mutex with attr or destroys the
    mutex, making it unusable in any form
  • SYNOPSIS
  • include ltpthread.hgt
  • int pthread_mutex_init(pthread_mutex_t mutex,
    const pthread_mutexattr_t attr) int
    pthread_mutex_destroy(pthread_mutex_t mutex)

5
Mutex unlock operation
  • NAME
  • pthread_mutex_unlock - unlocks mutex and wakes
    up the first thread sleeping on it.
  • SYNOPSIS
  • include ltpthread.hgt
  • int pthread_mutex_unlock(pthread_mutex_t
    mutex)

6
Mutex example
  • thread 1
  • thread 2
  • add(request_t request)
  • pthread_mutex_lock(lock)
  • request-gtnext requests requests request
    pthread_mutex_unlock(lock)
  • request_t remove()
  • pthread_mutex_lock(lock)
  • ...sleeping...
  • request requests
  • requests requests-gtnext
  • pthread_mutex_unlock(lock)
  • return(request)

7
Mutex example
8
Semaphores
  • A counting semaphore6 is a variable that you can
    increment arbitrarily high, but decrement only to
    zero. A sem_post() operation increments the
    semaphore, while a sem_wait() attempts to
    decrement it. If the semaphore is greater than
    zero, the operation succeeds if not, then the
    calling thread must go to sleep until a different
    thread increments it.

9
Semaphores initialisation
  • NAME
  • sem_init, sem_destroy - initializes the
    semaphore to value. If pshared is non-zero, then
    the semaphore will be sharable among processes.
    This destroys the semaphore.
  • SYNOPSIS
  • include ltpthread.hgt
  • int sem_init(sem_t sem, int pshared, unsigned
    int value)int sem_destroy(sem_t sem)

10
Semaphores operations
  • NAME
  • sem_post, sem_wait, sem_trywait - function
    increments the value of the semaphore or
    decrements the value of sem by one. If the
    semaphores value is zero, sem_wait() blocks,
    waiting for the semaphore to be incremented by
    another process or thread, while sem_trywait()
    will return immediately.
  • SYNOPSIS
  • include ltpthread.hgt
  • int sem_post(sem_t sem)
  • int sem_trywait(sem_t sem)
  • int sem_wait(sem_t sem)

11
Semaphores operations
12
Semaphores operations
  • NAME
  • sem_open, sem_close - returns a pointer to the
    semaphore name. All processes which call this on
    the same name will get the same semaphore pointer
    or closes the named semaphore for this process.
  • SYNOPSIS
  • include ltpthread.hgt
  • sem_t sem_open(char name, int oflag,... )
  • int sem_close(sem_t sem)

13
Semaphors example
14
Semaphors example
request_t get_request() request_t request
request (request_t ) malloc(sizeof(reque
st_t)) request-gtdata read_from_net()
return(request) void process_request(request_t
request) process(request-gtdata)
free(request)
producer() request_t request while(1)
request get_request()
add(request) sem_post(requests_length)
consumer() request_t request
while(1) SEM_WAIT(requests_length
) request remove() process_request(request)

15
Conditional Variables
16
Conditional Variable initialisation
  • NAME
  • pthread_cond_init, pthread_cond_destroy -
    initializes cond with att or destroys the
    condition variable, making it unusable in any
    form.
  • SYNOPSIS
  • include ltpthread.hgt
  • int pthread_cond_init(pthread_cond_t cond,
    const
  • pthread_condattr_t attr) int
    pthread_cond_destroy(pthread_cond_t cond)

17
Conditional Variable Wait Operation
  • NAME
  • pthread_cond_wait, pthread_cond_timewait -
    atomically releases mutex and causes the calling
    thread to block on cond. Upon successful return,
    the mutex will be reacquired.
  • SYNOPSIS
  • include ltpthread.hgt
  • int pthread_cond_wait(pthread_cond_t cond,
    pthread_mutex_t
  • mutex)int pthread_cond_timedwait(pthread_cond_
    t cond,
  • pthread_mutex_t mutex, const struct timespec
    abstime)

18
Conditional Variable Signal Operation
  • NAME
  • pthread_cond_signal, pthread_cond_broadcast -
    unblocks the first thread (if any) blocked on a
    condition variable or unblocks all threads
    blocked on a condition variable. You do not
  • know the order in which they awake.
  • SYNOPSIS
  • include ltpthread.hgt
  • int pthread_cond_signal(pthread_cond_t
    cond)int pthread_cond_broadcast(pthread_cond_t
    cond)

19
Conditional Variable Example
  • thread 1
  • thread 2
  • pthread_mutex_lock(m)
  • while (!my_condition)
  • pthread_cond_wait(c, m)
  • ... sleeping ...
  • do_thing()
  • pthread_mutex_unlock(m)
  • pthread_mutex_lock(m)
  • my_condition TRUE
  • pthread_mutex_unlock(m)
  • pthread_cond_signal(c)

20
Conditional Variable Example
21
Conditional Variable Example
  • void producer(void arg)
  • request_t request
  • while(1)
  • request get_request()
  • pthread_mutex_lock(r_lock)
  • while (length gt 10)
  • pthread_cond_wait(r_producer,
  • r_lock)
  • add(request)
  • length pthread_mutex_unlock(r_lock)
  • pthread_cond_signal(r_consumer)
  • void consumer(void arg)
  • request_t request
  • while(1)
  • pthread_mutex_lock(r_lock)
  • while (length 0)
  • pthread_cond_wait(r_consumer,
  • r_lock)
  • request remove()
  • length--
  • pthread_mutex_unlock(r_lock)
  • pthread_cond_signal(r_producer)
  • process_request(request)

22
Multithread APIsWindows OS
23
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

24
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

Pointer to a null-terminated string that
specifies the module to execute.
25
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

Pointer to a null-terminated string that
specifies the command line to execute.
26
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

A pointer to a SECURITY_ATTRIBUTES structure that
determines whether the returned handle can be
inherited by child processes.
27
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

If this parameter TRUE, each inheritable handle
in the calling process is inherited by the new
process.
28
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

Additional creation flags e.g. CREATE_NEW_CONSOLE,
CREATE_SUSPENDED, DETACHED_PROCESS
29
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

A pointer to the environment block for the new
process.
30
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

The full path to the current directory for the
process.
31
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

A pointer to a STARTUPINFO
32
Processes
  • NAME
  • CreateProcess - creates a new process and its
    primary thread.
  • SYNOPSIS
  • BOOL CreateProcess(
  • LPCTSTR lpApplicationName,
  • LPTSTR lpCommandLine,
  • LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  • BOOL bInheritHandles,
  • DWORD dwCreationFlags,
  • LPVOID lpEnvironment,
  • LPCTSTR lpCurrentDirectory,
  • LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMAT
    ION lpProcessInformation )

A pointer to a PROCESS_INFORMATION structure that
receives identification information about the new
process.
33
Process example
  • void _tmain( int argc, TCHAR argv )
  • STARTUPINFO si
  • PROCESS_INFORMATION pi
  • if( !CreateProcess( NULL, // No module name
    (use command line)
  • argv1, // Command line
  • NULL, // Process handle not inheritable
  • NULL, // Thread handle not inheritable
  • FALSE, // Set handle inheritance to FALSE
  • 0, // No creation flags
  • NULL, // Use parent's environment block
  • NULL, // Use parent's starting directory
  • si, // Pointer to STARTUPINFO structure
  • pi ) // Pointer to PROCESS_INFORMATION
    structure ) printf( "CreateProcess failed
    (d)\n", GetLastError() ) return
  • // Wait until child process exits.
  • WaitForSingleObject( pi.hProcess, INFINITE )
  • // Close process and thread handles.
  • CloseHandle( pi.hProcess )
  • CloseHandle( pi.hThread )

34
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

35
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

A pointer to a SECURITY_ATTRIBUTES structure that
determines whether the returned handle can be
inherited by child processes.
36
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

The initial size of the stack, in bytes.
37
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

A pointer to the application-defined function to
be executed by the thread and represents the
starting address of the thread. DWORD
ThreadProc(LPVOID lpParameter )
38
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

A pointer to a variable to be passed to the
thread.
39
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

The flags that control the creation of the thread
i.e. CREATE_SUSPENDED.
40
Threads
  • NAME
  • CreateThread- creates a thread to execute within
    the address space of the calling process.
  • SYNOPSIS
  • HANDLE CreateThread(
  • LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
    LPTHREAD_START_ROUTINE lpStartAddr,
  • LPVOID lpvThreadParam,
  • DWORD fdwCreate,
  • LPDWORD lpIDThread )

A pointer to a variable that receives the thread
identifier.
41
Threads - Example
  • int _tmain()
  • PMYDATA pDataMAX_THREADS
  • DWORD dwThreadIdMAX_THREADS
  • HANDLE hThreadMAX_THREADS
  • int i
  • for( i0 iltMAX_THREADS i )
  • pDatai (PMYDATA) HeapAlloc(GetProcessHeap(),
  • HEAP_ZERO_MEMORY, sizeof(MYDATA))
  • pData-gtval1 i pData-gtval2 i100
  • hThreadi CreateThread(
  • NULL, // default security attributes
  • 0, // use default stack size
  • MyThread, // thread function
  • pData, // argument to thread function
  • 0, // use default creation flags
  • dwThreadIdi) // returns the thread
    identifier
  • WaitForMultipleObjects(MAX_THREADS, hThread,
    TRUE, INFINITE) // Close all thread handles and
    free memory allocation.
  • for(i0 iltMAX_THREADS i)
    CloseHandle(hThreadi) HeapFree(GetProcessHeap
    (), 0, pDatai)

42
What is wrong with CreateThread function ?
  • A thread in an executable that calls the C
    run-time library (CRT) should use the
    _beginthreadex and _endthreadex functions for
    thread management rather than CreateThread and
    ExitThread

43
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

44
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Pointer to a SECURITY_ATTRIBUTES structure that
determines whether the returned handle can be
inherited by child processes.
45
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Stack size for a new thread or 0.
46
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Start address of a routine that begins execution
of a new thread.
47
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Argument list to be passed to a new thread.
48
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Argument list to be passed to a new thread.
49
_beginthreadex
  • NAME
  • _beginthreadex - creates a thread to execute
    within the address space of the calling process.
  • SYNOPSIS
  • uintptr_t _beginthreadex(
  • void security,
  • unsigned stack_size,
  • unsigned ( start_address )( void ),
  • void arglist,
  • unsigned initflag,
  • unsigned thrdaddr )

Points to a 32-bit variable that receives the
thread identifierlist to be passed to a new
thread.
50
_beginthreadex example
  • int main()
  • HANDLE hThread
  • unsigned threadID
  • printf( "Creating second thread...\n" )
  • hThread (HANDLE)_beginthreadex(
  • NULL,
  • 0,
  • SecondThreadFunc,
  • NULL,
  • 0,
  • threadID )
  • WaitForSingleObject( hThread, INFINITE )
  • CloseHandle( hThread )

51
ExitThread or return
  • ExitThread is the preferred method of exiting a
    thread in C code.
  • Return is the preferred method of exiting a
    thread in C code.

52
Synchronisation
  • Synchronisation in user mode
  • Atomic access
  • Critical sections
  • Synchronisation in kernel mode
  • Wait functions
  • Mutexes
  • Semphores

53
Synchronisation in user mode
54
Interlock functions
  • InterlockedAdd
  • InterlockedAnd
  • InterlockedBitTestAndReset
  • InterlockedBitTestAndSet
  • InterlockedCompareExchangePointer
  • InterlockedDecrement
  • InterlockedExchange
  • InterlockedExchangeAdd
  • InterlockedExchangePointer
  • InterlockedIncrement
  • InterlockedOr 
  • InterlockedXor 

55
Interlock functions
  • InterlockedAdd
  • InterlockedAnd
  • InterlockedBitTestAndReset
  • InterlockedBitTestAndSet
  • InterlockedCompareExchangePointer
  • InterlockedDecrement
  • InterlockedExchange
  • InterlockedExchangeAdd
  • InterlockedExchangePointer
  • InterlockedIncrement
  • InterlockedOr 
  • InterlockedXor 

LONG InterlockedAdd(LONG volatile Addend, LONG
Value ) Performs an atomic addition operation on
the specified LONG values.
56
Interlock functions
  • InterlockedAdd
  • InterlockedAnd
  • InterlockedBitTestAndReset
  • InterlockedBitTestAndSet
  • InterlockedCompareExchangePointer
  • InterlockedDecrement
  • InterlockedExchange
  • InterlockedExchangeAdd
  • InterlockedExchangePointer
  • InterlockedIncrement
  • InterlockedOr 
  • InterlockedXor 

BOOLEAN InterlockedBitTestAndSet(LONG volatile
Base, LONG Bit ) Tests the specified bit of
the specified LONG value and sets it to 1. The
operation is atomic.
57
Interlock functions
  • InterlockedAdd
  • InterlockedAnd
  • InterlockedBitTestAndReset
  • InterlockedBitTestAndSet
  • InterlockedCompareExchangePointer
  • InterlockedDecrement
  • InterlockedExchange
  • InterlockedExchangeAdd
  • InterlockedExchangePointer
  • InterlockedIncrement
  • InterlockedOr 
  • InterlockedXor 

PVOID InterlockedCompareExchangePointer(PVOID
volatile Destination, PVOID Exchange, PVOID
Comparand ) The function compares the
Destination value with the Comparand value. If
the Destination value is equal to the Comparand
value, the Exchange value is stored in the
address specified by Destination. Otherwise, no
operation is performed.
58
Interlock functions
  • InterlockedAdd
  • InterlockedAnd
  • InterlockedBitTestAndReset
  • InterlockedBitTestAndSet
  • InterlockedCompareExchangePointer
  • InterlockedDecrement
  • InterlockedExchange
  • InterlockedExchangeAdd
  • InterlockedExchangePointer
  • InterlockedIncrement
  • InterlockedOr 
  • InterlockedXor 

LONG InterlockedIncrement(PLONG  Addend) Incremen
ts a caller-supplied variable as an atomic
operation.
59
Critical sections
  • void InitializeCriticalSection(
  • LPCRITICAL_SECTION lpCriticalSection )
  • Initializes a critical section object.
  • BOOL InitializeCriticalSectionAndSpinCount(
    LPCRITICAL_SECTION lpCriticalSection, DWORD
    dwSpinCount )
  • Initializes a critical section object and sets
    the spin count for the critical section.

60
Critical sections
  • void EnterCriticalSection(
  • LPCRITICAL_SECTION pCriticalSection )
  • Waits for ownership of the specified critical
    section object. The function returns when the
    calling thread is granted ownership.
  • void LeaveCriticalSection( LPCRITICAL_SECTION
    lpCriticalSection )
  • Releases ownership of the specified critical
    section object.

61
Critical sections example
  • CRITICAL_SECTION CriticalSection
  • int main()
  • if (!InitializeCriticalSectionAndSpinCount(Criti
    calSection, 0x80000400) ) return 0 ...
  • // Release resources used by the critical
    section object. DeleteCriticalSection(CriticalSec
    tion)
  • DWORD ThreadProc( LPVOID lpParameter )
  • ...
  • // Request ownership of the critical section.
    EnterCriticalSection(CriticalSection)
  • // Access the shared resource.
  • // Release ownership of the critical section.
    LeaveCriticalSection(CriticalSection)
  • ...

62
Synchronisation in kernel mode
63
Wait Functions
  • NAME
  • WaitForSingleObject - Waits until the specified
    object is in the signaled state or the time-out
    interval elapses.
  • SYNOPSIS
  • DWORD WaitForSingleObject(HANDLE hHandle, DWORD
    dwMilliseconds )

64
Wait Functions
  • NAME
  • WaitForMultipleObjects - Waits until one or all
    of the specified objects are in the signaled
    state or the time-out interval elapses.
  • SYNOPSIS
  • DWORD WaitForMultipleObjects(DWORD nCount, const
    HANDLE lpHandles, BOOL bWaitAll, DWORD
    dwMilliseconds )

65
CloseHandle
  • NAME
  • CloseHandle - Closes an open object handle.
  • SYNOPSIS
  • BOOL CloseHandle(HANDLE hObject )

66
Events Objects
  • The event object is useful in sending a signal to
    a thread indicating that a particular event has
    occurred.

67
Event Functions
  • CreateEvent
  • OpenEvent
  • SetEvent
  • ResetEvent

68
CreateEvent
  • NAME
  • CreateEvent- Creates or opens a named or unnamed
    event object.
  • SYNOPSIS
  • HANDLE CreateEvent(
  • LPSECURITY_ATTRIBUTES lpEventAttributes,
  • BOOL bManualReset,
  • BOOL bInitialState,
  • LPCTSTR lpName )

69
ResetEvent
  • NAME
  • ResetEvent- Sets the specified event object to
    the nonsignaled state.
  • SYNOPSIS
  • BOOL ResetEvent(HANDLE hEvent )

70
SetEvent
  • NAME
  • ResetEvent- Sets the specified event object to
    the signaled state.
  • SYNOPSIS
  • BOOL SetEvent(HANDLE hEvent )

71
Event Example
  • HANDLE ghGlobalWriteEvent
  • HANDLE ghReadEventsTHREADCOUNT
  • void WriteToBuffer(VOID)
  • DWORD dwWaitResult, i
  • ResetEvent(ghGlobalWriteEvent) )
  • dwWaitResult WaitForMultipleObjects(
  • THREADCOUNT, // number of handles in
    array
  • ghReadEvents, // array of read-event
    handles
  • TRUE, // wait until all are
    signaled
  • INFINITE) // indefinite wait
  • switch (dwWaitResult)
  • case WAIT_OBJECT_0
  • printf("Main thread writing to the shared
    buffer...\n")
  • break
  • default
  • printf("Wait error d\n",
    GetLastError())

DWORD ThreadProc(LPVOID lpParam) DWORD
dwWaitResult HANDLE hEvents2
hEvents0 (HANDLE)lpParam // thread's read
event hEvents1 ghGlobalWriteEvent //
global write event dwWaitResult
WaitForMultipleObjects( 2, //
number of handles in array hEvents,
// array of event handles TRUE,
// wait till all are signaled INFINITE)
// indefinite wait switch (dwWaitResult)
case WAIT_OBJECT_0
printf("Thread d reading from buffer...\n",
GetCurrentThreadId())
break default
printf("Wait error d\n", GetLastError())
ExitThread(0)
SetEvent(hEvents0) return 1
72
Semaphores
  • NAME
  • CreateSemaphore - Creates or opens a named or
    unnamed semaphore object.
  • SYNOPSIS
  • HANDLE CreateSemaphore(
  • LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
    LONG lInitialCount,
  • LONG lMaximumCount,
  • LPCTSTR lpName )

73
Semaphores
  • NAME
  • ReleaseSemaphore - Increases the count of the
    specified semaphore object by a specified amount.
  • SYNOPSIS
  • BOOL ReleaseSemaphore(
  • HANDLE hSemaphore,
  • LONG lReleaseCount,
  • LPLONG lpPreviousCount )

74
Semaphore example
  • HANDLE ghSemaphore
  • void main()
  • HANDLE aThreadTHREADCOUNT
  • DWORD ThreadID
  • int i
  • ghSemaphore CreateSemaphore(
  • NULL, // default security
    attributes
  • MAX_SEM_COUNT, // initial count
  • MAX_SEM_COUNT, // maximum count
  • NULL) // unnamed semaphore
  • for( i0 i lt THREADCOUNT i )
  • aThreadi CreateThread(
  • NULL, // default
    security attributes
  • 0, // default stack
    size
  • (LPTHREAD_START_ROUTINE)
    ThreadProc,
  • NULL, // no thread
    function arguments
  • 0, // default
    creation flags
  • DWORD WINAPI ThreadProc( LPVOID lpParam )
  • DWORD dwWaitResult
  • BOOL bContinueTRUE
  • while(bContinue)
  • dwWaitResult WaitForSingleObject(
  • ghSemaphore, // handle to semaphore
  • 0L) // zero-second
    time-out interval
  • switch (dwWaitResult)
  • case WAIT_OBJECT_0
  • bContinueFALSE
  • Sleep(5)
  • ReleaseSemaphore(
  • ghSemaphore, // handle
    to semaphore
  • 1, // increase
    count by one
  • NULL) ) // not
    interested in previous count
  • break

75
Mutex
  • NAME
  • CreateMutex- Creates or opens a named or unnamed
    mutex object.
  • SYNOPSIS
  • HANDLE CreateMutex(
  • PSECURITY_ATTRIBUTES lpMutexAttributes, BOOL
    bInitialOwner,
  • LPCTSTR lpName )

76
Mutex
  • NAME
  • ReleaseMutex - Releases ownership of the
    specified mutex object.
  • SYNOPSIS
  • BOOL ReleaseMutex(HANDLE hMutex )

77
Mutex example
  • HANDLE ghMutex
  • void main()
  • HANDLE aThreadTHREADCOUNT
  • DWORD ThreadID
  • int i
  • ghMutex CreateMutex(
  • NULL, // default security
    attributes
  • FALSE, // initially not owned
  • NULL) // unnamed mutex
  • for( i0 i lt THREADCOUNT i )
  • aThreadi CreateThread(
  • NULL, // default
    security attributes
  • 0, // default stack
    size
  • (LPTHREAD_START_ROUTINE)
    WriteToDatabase,
  • NULL, // no thread
    function arguments
  • 0, // default
    creation flags
  • ThreadID) // receive
    thread identifier

DWORD WINAPI WriteToDatabase( LPVOID lpParam )
DWORD dwCount0, dwWaitResult while(
dwCount lt 20 ) dwWaitResult
WaitForSingleObject( ghMutex, //
handle to mutex INFINITE) // no
time-out interval switch (dwWaitResult)
case WAIT_OBJECT_0
__try
printf("Thread d writing to database...\n",
GetCurrentThreadId())
dwCount
__finally if
(! ReleaseMutex(ghMutex))
// Deal with error.

break case WAIT_ABANDONED
return FALSE
return TRUE
78
Advanced Thread Objects
  • Thread pools
  • Fibers

79
Thank You
Write a Comment
User Comments (0)
About PowerShow.com