中央論壇 - CENTER BBS
標題:
C++多執行緒(七)
[打印本頁]
作者:
f66666602
時間:
2007-8-14 04:46
標題:
C++多執行緒(七)
一 信標Semaphore
信標內核對像用於對資源進行計數。它們與所有內核對像一樣,包含一個使用數量,但是它們也包含另外兩個帶符號的3 2位值,一個是最大資源數量,一個是當前資源數量。最大資源數量用於標識信標能夠控制的資源的最大數量,而當前資源數量則用於標識當前可以使用的資源的數量。
為了正確地說明這個問題,讓我們來看一看應用程序是如何使用信標的。比如說,我正在開發一個服務器進程,在這個進程中,我已經分配了一個能夠用來存放客戶機請求的緩衝區。我對緩衝區的大小進行了硬編碼,這樣它每次最多能夠存放5個客戶機請求。如果5個請求尚未處理完畢時,一個新客戶機試圖與服務器進行聯繫,那麼這個新客戶機的請求就會被拒絕,並出現一個錯誤,指明服務器現在很忙,客戶機應該過些時候重新進行聯繫。當我的服務器進程初始化時,它創建一個執行緒池,裡面包含5個執行緒,每個執行緒都準備在客戶機請求到來時對它進行處理。
開始時,沒有客戶機提出任何請求,因此我的服務器不允許執行緒池中的任何執行緒成為可調度執行緒。但是,如果3個客戶機請求同時到來,那麼執行緒池中應該有3個執行緒處於可調度狀態。使用信標,就能夠很好地處理對資源的監控和對執行緒的調度,最大資源數量設置為5,因為這是我進行硬編碼的緩衝區的大小。當前資源數量最初設置為0,因為沒有客戶機提出任何請求。當客戶機的請求被接受時,當前資源數量就遞增,當客戶機的請求被提交給服務器的執行緒池時,當前資源數量就遞減。
信標的使用規則如下:
. 如果當前資源的數量大於0,則發出信標信號。
. 如果當前資源數量是0,則不發出信標信號。
. 系統決不允許當前資源的數量為負值。
. 當前資源數量決不能大於最大資源數量。
當使用信標時,不要將信標對象的使用數量與它的當前資源數量混為一談。
二 API
[tr] Semaphore function Description [/tr] CreateSemaphore Creates or opens a named or unnamed semaphore object.
CreateSemaphoreEx Creates or opens a named or unnamed semaphore object and returns a handle to the object.
OpenSemaphore Opens an existing named semaphore object.
ReleaseSemaphore Increases the count of the specified semaphore object by a specified amount.
三 實例
#include <windows.h>
#include <stdio.h>
#define MAX_SEM_COUNT 6
#define THREADCOUNT 12
HANDLE ghSemaphore;
DWORD WINAPI ThreadProc( LPVOID );
void main()
{
HANDLE aThread[THREADCOUNT];
DWORD ThreadID;
int i;
// Create a semaphore with initial and max counts of MAX_SEM_COUNT
ghSemaphore = CreateSemaphore(
NULL, // default security attributes
MAX_SEM_COUNT, // initial count
MAX_SEM_COUNT, // maximum count
NULL); // unnamed semaphore
if (ghSemaphore == NULL)
{
printf("CreateSemaphore error: %d\n"、GetLastError());
return;
}
// Create worker threads
for( i=0; i < THREADCOUNT; i++ )
{
aThread = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ThreadProc、
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
if( aThread == NULL )
{
printf("CreateThread error: %d\n"、GetLastError());
return;
}
}
// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT、aThread、TRUE、INFINITE);
// Close thread and semaphore handles
for( i=0; i < THREADCOUNT; i++ )
CloseHandle(aThread);
CloseHandle(ghSemaphore);
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
DWORD dwWaitResult;
BOOL bContinue=TRUE;
while(bContinue)
{
// Try to enter the semaphore gate.
dwWaitResult = WaitForSingleObject(
ghSemaphore, // handle to semaphore
3L); // zero-second time-out interval
switch (dwWaitResult)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
// TODO: Perform task
printf("Thread %d: wait succeeded\n"、GetCurrentThreadId());
bContinue=FALSE;
// Simulate thread spending time on task
Sleep(5);
for(int x =
0; x<
10; x++)
printf("Thread %d task!\n",GetCurrentThreadId());
// Relase the semaphore when task is finished
if (!ReleaseSemaphore(
ghSemaphore, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
printf("ReleaseSemaphore error: %d\n"、GetLastError());
}
break;
// The semaphore was nonsignaled、so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n"、GetCurrentThreadId());
break;
}
}
return TRUE;
}
四 參考 [url]http://msdn2.microsoft.com/en-us/library/ms686946.aspx[/url]
複製代碼
歡迎光臨 中央論壇 - CENTER BBS (https://www.centerbbs.com/)
Powered by Discuz! X3