René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Manual Reset Events vs Auto Reset Events

If an event is a manual reset event, all WaitFor...Object Functions return that wait for that event. This is different for auto reset events: only one of the Wait Functions return and the Event is immediately unsignalled.
#include <stdio.h>
#include <process.h>
#include <windows.h>

unsigned __stdcall TF(void* arg) {

  HANDLE ev=*(HANDLE*) arg;

  WaitForSingleObject(ev,INFINITE);

  printf("Wait ended\n");

  return 0;
}

int main(int argc, char* argv[]){
  if (argc < 2) {
    printf ("%s auto|manual\n", argv[0]);
    exit (0);
  }

  bool manualReset;
  if      (strcmpi(argv[1],"auto"  )) manualReset=false;
  else if (strcmpi(argv[1],"manual")) manualReset=true;
  else {
    printf ("%s auto|manual\n", argv[0]);
    exit (0);
  }

  HANDLE ev = CreateEvent(
    0,
    manualReset,
    false,  // not signalled
    0);

  printf ("Starting three threads\n");

  _beginthreadex(0,0,TF,(void*) &ev,0,0);
  _beginthreadex(0,0,TF,(void*) &ev,0,0);
  _beginthreadex(0,0,TF,(void*) &ev,0,0);

  printf ("Sleeping a second\n");

  Sleep(1000);
 
  printf ("Signalling the event\n");

  // Signal the Event
  SetEvent(ev);

  // wait forever
  while(1);

  return 0;
}