Create_Semaphore.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 3k
Development Platform:

C++ Builder

  1. #include <genstub.cpp>
  2. // This is a child thread procedure that waits for a semaphore,
  3. // holds the semaphore for five seconds, and releases the semaphore.
  4. // Threads that cannot get semaphores will wait until other threads exit.
  5. DWORD WINAPI ChildThreadProc( HWND hWnd )
  6. {
  7.     TCHAR szBuffer[256];                            // buffer
  8.     DWORD dwSemCount = 0;                           // printing semaphore count
  9.     HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, "TEST_SEMAPHORE" );
  10.     wsprintf( szBuffer,"Thread %x waiting for semaphore %x",
  11.              GetCurrentThreadId( ), hSemaphore );
  12.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  13.     // Check for signaled semaphore.
  14.     WaitForSingleObject( hSemaphore, INFINITE );
  15.     wsprintf( szBuffer,"Thread %x got semaphore", GetCurrentThreadId( ) );
  16.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  17.     Sleep( 5000 );
  18.     // Release semaphore.
  19.     ReleaseSemaphore( hSemaphore, 1, &dwSemCount );
  20.     wsprintf( szBuffer,"Thread %x is done with semaphore. Its count was %ld.",
  21.             GetCurrentThreadId( ), dwSemCount );
  22.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  23.     CloseHandle( hSemaphore );
  24.     ExitThread( TRUE );
  25. }
  26. // Windows message procedure.
  27. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29.    static HANDLE hSemaphore = 0;
  30.    switch (uMsg)
  31.    {
  32.            case WM_CREATE:
  33.                    hSemaphore = CreateSemaphore( NULL, 4, 4, "TEST_SEMAPHORE" );
  34.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  35.            case WM_COMMAND:       // process menu items
  36.                    switch ( LOWORD( wParam )  )
  37.                    {
  38.                       case IDM_TEST:     // start up a thread.
  39.                         {
  40.                            DWORD dwChildId;
  41.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId );
  42.                         }
  43.                         break;
  44.                       case IDM_EXIT:
  45.                            DestroyWindow( hWnd );
  46.                            break;
  47.                    }
  48.            break;
  49.            case WM_USER:
  50.                    {  // Message to show synchronization actions.
  51.                       TCHAR szBuffer[101];
  52.                       static int row = 0;
  53.                       static int msg_num = 1;
  54.                       HDC hDC = GetDC( hWnd );
  55.                       FillMemory( szBuffer, 100, 32 );
  56.                       TextOut( hDC, 0, row, szBuffer, 100 );
  57.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  58.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  59.                       if ( row > 200 )
  60.                          row = 0;
  61.                       else
  62.                          row += 20;
  63.                       ReleaseDC( hWnd, hDC );
  64.                    }
  65.                    break;
  66.            case WM_DESTROY:
  67.                    if ( hSemaphore )
  68.                       CloseHandle( hSemaphore );
  69.                    PostQuitMessage( 0 );
  70.                    break;
  71.            default:
  72.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  73.    }
  74.    return NULL;
  75. }