//============================================== file = suspendThread.c =====
//=  A simple bare-bones example of Windows threads with suspend            =
//===========================================================================
//=  Notes:                                                                 =
//=    1) This program demonstrates how a thread can be resumed and         =
//=       suspended periodically (from a timing thread).                    =
//=    2) Need to set T_ON and T_OFF in #define                             =
//=    3) Ignore two compile-time warnings on unused parameter              =
//=-------------------------------------------------------------------------=
//= Example execution: (T_ON = 2000, T_OFF = 5000)                          =
//=                                                                         =
//=    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20           =
//=   21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40           =
//=   41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60           =
//=   61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80           =
//=   81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100          =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 -WM suspendThread.c                                       =
//=-------------------------------------------------------------------------=
//=  Execute: suspendThread                                                 =
//=-------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (02/28/11) - Genesis (from semaphore2.c)                  =
//===========================================================================
//----- Include files -------------------------------------------------------
#include <stdio.h>                // Needed for printf()
#include <windows.h>              // Needed for windows stuff

//----- Defines -------------------------------------------------------------
#define    TRUE      1            // Boolean true
#define   FALSE      0            // Boolean false
#define    T_ON   2000            // Time to be on (milliseconds)
#define    T_OFF  5000            // Time to be off (milliseconds

//----- Globals -------------------------------------------------------------
HANDLE Athread;                   // Thread handle a
HANDLE Bthread;                   // Thread handle b

//----- Function prototypes -------------------------------------------------
DWORD WINAPI printThread(LPVOID);    // Thread to print
DWORD WINAPI timingThread(LPVOID);   // Thread to periodically pause print

//===========================================================================
//=  Main program                                                           =
//===========================================================================
int main(void)
{
  DWORD ThreadID;      // Thread ID

  // Create the timing thread
  Athread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) timingThread, NULL, 0, &ThreadID);
  if(Athread == NULL)
  {
    printf("*** ERROR - CreateThread() failed \n");
    exit(1);
  }

  // Sleep for 1 millisecond to allow timingThread to start
  Sleep(1);

  // Create the print thread
  Bthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) printThread, NULL, 0, &ThreadID);
  if(Bthread == NULL)
  {
    printf("*** ERROR - CreateThread() failed \n");
    exit(1);
  }

  // Sleep for 30 seconds while threads run
  Sleep(30000);

  // Close thread and semaphore handles
  CloseHandle(Athread);
  CloseHandle(Bthread);

  // Return
  return(0);
}

//===========================================================================
//=  This is is the print thread function                                   =
//===========================================================================
DWORD WINAPI printThread(LPVOID lpParam)
{
  int index;              // Index for print loop

  // Loop forever
  index = 0;
  while(1)
  {
    index++;
    printf("%2d ", index);
    Sleep(101);
  }
}

//===========================================================================
//=  This is is the timing thread function                                  =
//===========================================================================
DWORD WINAPI timingThread(LPVOID lpParam)
{
  // Loop forever
  while(1)
  {
    // Sleep for T_ON milliseconds for the ON period
    ResumeThread(Bthread);
    Sleep(T_ON);

    // Sleep for T_OFF milliseconds for the OFF period
    SuspendThread(Bthread);
    Sleep(T_OFF);
    printf("\n");
  }
}

