Beruflich Dokumente
Kultur Dokumente
Concurrent Programming
Lecture 21:
Concurrency in Other
Environments (Part 1)
Dr. Richard S. Hall
rickhall@cs.tufts.edu
Clement Escoffier
clement.escoffier@gmail.com
Agenda
Agenda
Pthreads Recommendation
Pthreads Overview
Thread management
Mutex management
Condition variables
Common prefixes
Thread Creation
p
pthread_create(thread,attr,start_routine,arg)
Arguments
Thread Attributes
pthread_attr_init(attr);
pthread_attr_destroy(attr);
pthread_attr_getdetachstate(attr,detachstate);
pthread_attr_setdetachstate(attr,detachstate);
Thread Termination
pthread_exit(status);
Stops a thread
pthread_join(threadid,status);
p
pthread_self()
p
pthread_equal(thread1,thread2)
p
pthread_yield()
Mutex Creation
p
pthread_mutex_init(mutex,attr)
100
";
http://www.oopweb.com/CPP/Documents/CPPHOWT
O/Volume/C++Programming-HOWTO-18.html
Pthreads Summary
Agenda
Thread on Windows
The old-fashion
MFC (Microsoft Foundation Class)
OpenThread (since Windows 98)
New improvements in Vista
Thread Scheduling
Thread management
Mutex management
Condition variables
Thread Library
Header:
include <windows.h>
Thread Creation
HANDLE CreateThread(
__in LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in DWORD dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out LPDWORD lpThreadIdjl
);
Arguments
Thread Creation
Thread procedure
0,
0,
0,
0,
Thread1,
Thread1,
Thread1,
Thread1,
1,
2,
3,
4,
0,
0,
0,
0,
&threadID1);
&threadID2);
&threadID3);
&threadID4);
Stopping/Resuming Threads
Function to stop the calling thread
The thread is exited before any destructors can be called or any other automatic
cleanup can be performed. Therefore, you should return from your thread function.
Thread Synchronization
Using Events
CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
BOOL bManualReset, // reset type
BOOL bInitialState, // initial state
LPCTSTR lpName // object name
Using Events
Unsignal event:
BOOL ResetEvent(
__in HANDLE hEvent // handle to event
);
WaitForSingleObject(), WaitForMultipleObjects(),
SignalObjectAndWait(), WaitForMultipleObjectsEx()
S
MsgWaitForMultipleObjects(), MsgWaitForMultipleObjectsEx,
MsgWaitForMultipleObjectsEx(), SignalObjectAndWait(),
WaitForMultipleObjectsEx(), WaitForSingleObjectEx()
W
R
RegisterWaitForSingleObject()
Using Events
HANDLE g_event;
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
printf("This is my thread\n");
SetEvent(g_event); // Once done set the event to
signaled
}
int main() {
g_event=CreateEvent(NULL, 0, 0, NULL); // Create the
event
DWORD threadID;
CreateThread(NULL, 0, Thread1, NULL, 0, &threadID);
WaitForSingleObject(g_event, INFINITE); // Wait for
the event
return 0;
}
Main
// Create a mutex with no initial owner
ghMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
);
Using Semaphores
Condition Variables
SleepConidtionVariableCS
SleepConditionVariableSRW
WakeAllConditionVariable
WakeConditionVariable
Condition Variables
The producer/consumer example
EnterCriticalSection (&BufferLock);
while (QueueSize == BUFFER_SIZE && StopRequested == FALSE){
// Buffer is full - sleep so consumers can get items.
SleepConditionVariableCS (
&BufferNotFull, &BufferLock, INFINITE);
}
if (StopRequested == TRUE) {
LeaveCriticalSection (&BufferLock);
return; }
// Add the produced element
WakeConditionVariable (&BufferNotEmpty);
LeaveCriticalSection (&BufferLock);
}
Conclusion
To go further :
MSDN : http://msdn.com