// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>

using namespace std;

#include <windows.h>

CRITICAL_SECTION cs;

DWORD __stdcall tfunc(void *p)
{
   int *arg = (int *)p;
   for(int i=0;i<10;i++)
   {
      EnterCriticalSection(&cs); 
      cout << *arg << endl;
      LeaveCriticalSection(&cs);
      Sleep(1000);
   }
   return 0;
}

int main()
{
   InitializeCriticalSection(&cs);   
   int arg1 = 123;
   DWORD id1;
   HANDLE h1 = CreateThread(NULL,0,tfunc,&arg1,0,&id1);
   int arg2 = 456;
   DWORD id2;
   HANDLE h2 = CreateThread(NULL,0,tfunc,&arg2,0,&id2);
   WaitForSingleObject(h1,INFINITE);
   WaitForSingleObject(h2,INFINITE);
   return 0;
}
