一、多線程體繫結構
主線程建立程式所需要的所有視窗,並包括所有的視窗過程,以便處理這些視窗的所有訊息;其他線程只進行一些幕後處理,除了與主線程進行通訊,他們不與使用者進行交流。
超過1/10秒的事件(大作業,如拼字檢測、資料庫排序、列印)不應該放在訊息佇列中,而應該放在單獨的線程中處理。
線上程終止時,線程建立的資源不會自動釋放。
二、多線程API:
hThread = CreateThread(...)
DWORD WINAPI ThreadProc([PVOID pParam)
_beginthread(...)
void __cdecl ThreadProc(void* pParam)///在此函數結尾一般有_endthread();
ResumeThread //恢複線程
Sleep(0)//線程交出尚未完成的時間片。
三、MSDN
Synchronization Functions
The following functions are used in synchronization.
| Asynchronous Function |
Description |
| APCProc |
An application-defined callback function used with the QueueUserAPC function. |
| GetOverlappedResult |
Retrieves the results of an overlapped operation. |
| QueueUserAPC |
Adds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread. |
| Critical-Section Function//臨界區 |
Description |
| DeleteCriticalSection |
Releases all resources used by an unowned critical section object. |
| EnterCriticalSection |
Waits for ownership of the specified critical section object. |
| InitializeCriticalSection |
Initializes a critical section object. |
| InitializeCriticalSectionAndSpinCount |
Initializes a critical section object and sets the spin count for the critical section. |
| LeaveCriticalSection |
Releases ownership of the specified critical section object. |
| SetCriticalSectionSpinCount |
Sets the spin count for the specified critical section. |
| TryEnterCriticalSection |
Attempts to enter a critical section without blocking. |
| Event Function//事件對象 |
Description |
| CreateEvent |
Creates or opens a named or unnamed event object. |
| OpenEvent |
Opens an existing named event object. |
| PulseEvent |
Sets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads. |
| ResetEvent |
Sets the specified event object to the nonsignaled state. |
| SetEvent |
Sets the specified event object to the signaled state. |
| Interlocked Function |
Description |
| InterlockedCompareExchange |
Performs an atomic comparison of the specified values and exchanges the values, based on the outcome of the comparison. |
| InterlockedCompareExchangePointer |
Performs an atomic comparison of the specified values and exchange of the values, based on the outcome of the comparison. |
| InterlockedDecrement |
Decrements (decreases by one) the value of the specified variable and checks the resulting value. |
| InterlockedExchange |
Atomically exchanges a pair of values. |
| InterlockedExchangeAdd |
Performs an atomic addition of an increment value to an addend variable. |
| InterlockedExchangePointer |
Atomically exchanges a pair of values. |
| InterlockedIncrement |
Increments (increases by one) the value of the specified variable and checks the resulting value. |
| Mutex Function//互斥 |
Description |
| CreateMutex |
Creates or opens a named or unnamed mutex object. |
| OpenMutex |
Opens an existing named mutex object. |
| ReleaseMutex |
Releases ownership of the specified mutex object. |
| Semaphore Function |
Description |
| CreateSemaphore |
Creates or opens a named or unnamed semaphore object. |
| OpenSemaphore |
Opens an existing named semaphore object. |
| ReleaseSemaphore |
Increases the count of the specified semaphore object by a specified amount. |
| Timer-Queue Timer Function |
Description |
| ChangeTimerQueueTimer |
Updates a timer-queue timer. |
| CreateTimerQueue |
Creates a queue for timers. |
| CreateTimerQueueTimer |
Creates a timer-queue timer. |
| DeleteTimerQueue |
Deletes a timer queue. |
| DeleteTimerQueueEx |
Deletes a timer queue. |
| DeleteTimerQueueTimer |
Cancels a timer-queue timer. |
| Wait Function |
Description |
| MsgWaitForMultipleObjects |
Returns when the specified criteria for the specified objects is met. |
| MsgWaitForMultipleObjectsEx |
Returns when the specified criteria for the specified objects is met. |
| RegisterWaitForSingleObject |
Directs a wait thread in the thread pool to wait on the object. |
| SignalObjectAndWait |
Allows the caller to atomically signal an object and wait on another object. |
| UnregisterWait |
Cancels a registered wait operation. |
| UnregisterWaitEx |
Cancels a registered wait operation. |
| WaitForMultipleObjects |
Returns when the specified criteria for the specified objects is met. |
| WaitForMultipleObjectsEx |
Returns when the specified criteria for the specified objects is met. |
| WaitForSingleObject |
Returns when the specified criteria for the specified object is met. |
| WaitForSingleObjectEx |
Returns when the specified criteria for the specified object is met. |
| WaitOrTimerCallback |
Returns when the specified criteria is met. |
| Waitable Timer Function |
Description |
| CancelWaitableTimer |
Sets the specified waitable timer to the inactive state. |
| CreateWaitableTimer |
Creates or opens a waitable timer object. |
| OpenWaitableTimer |
Opens an existing named waitable timer object. |
| SetWaitableTimer |
Activates the specified waitable timer. |
| TimerAPCProc |
Application-defined timer completion routine used with the SetWaitableTimer function. |
=================
註解。
臨界區,任意時刻只有一個線程可以進入臨界區:
init: BOOL isused = false; //表示沒線程進入臨界區
enter: while(isused){sleep(1);} isused=true;
leave: isused = false;
臨界區只能用於同一進程內不同線程間的協調。不同進程間的協調,要用互斥。
事件對象的用法:
初始化
hEvent = CreateEvent (NULL, FALSE, FALSE, NULL) ;
beginthread (Thread, 0, ¶ms) ;
線程函數:
void Thread (PVOID pvoid)
{
hEvent = ...//從pvoid中獲得
while (TRUE)
{
WaitForSingleObject (hEvent, INFINITE) ;
//。。。。。。。。。。主體
}
}
開始執行:
SetEvent (hEvent) ;