
  '----------------
  'MULTIPLE THREADS
  '================

  'http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx


  #define FALSE    0
  #define TRUE     1
  #define IGNORE   0           //Ignore signal
  #define INFINITE 0xFFFFFFFF  //Infinite timeout

  #define __in
  #define __out
  #define __in_opt
  #define __out_opt
  #define __in_ecount(nCount)

  #define MAX_THREADS 3

  'Sample custom data structure for threads to use.
  'This is passed by void pointer so it can be any data type
  'that can be passed using a single void pointer (LPVOID).
  '
  type dtype sys val1,val2

  typedef sys   HANDLE,SIZE_T
  typedef void  *LPVOID, *LPSECURITY_ATTRIBUTES, *LPTHREAD_START_ROUTINE
  typedef dword *LPDWORD

  '================================
  extern stdcall lib "kernel32.dll"
  '================================


  HANDLE CreateThread
  (
    __in_opt  LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in      SIZE_T dwStackSize,
    __in      LPTHREAD_START_ROUTINE lpStartAddress,
    __in_opt  LPVOID lpParameter,
    __in      DWORD dwCreationFlags,
    __out_opt LPDWORD lpThreadId
  )


  DWORD WaitForMultipleObjects
  (
    __in DWORD nCount,
    __in_ecount(nCount) CONST HANDLE *lpHandles,
    __in BOOL bWaitAll,
    __in DWORD dwMilliseconds
  )


  BOOL CloseHandle
  (
    __in HANDLE hObject
  )


  end extern

  sys counter

  function ThreadA( dtype*pd ) as sys external
  ============================================
  finit
  inc counter
  print "Thread A: " pd.val1 "    " pd.val2 "    " + counter
  return 0 
  end function 

  function ThreadB( dtype*pd ) as sys external
  ============================================
  finit
  inc counter
  print "Thread B: " pd.val1 "    " pd.val2 "    " + counter
  return 0 
  end function 

  function ThreadC( dtype*pd ) as sys external
  ============================================
  finit
  inc counter
  print "Thread C: " pd.val1 "    " pd.val2 "    " + counter
  return 0 
  end function 

  #define MAX_THREADS 3

 'LPTHREAD_START_ROUTINE FunArray[MAX_THREADS]<=(&ThreadA,&ThreadB,&ThreadC)
  sys FunArray={&ThreadA,&ThreadB,&ThreadC}
print 12


  function main()
  '==============

  dtype  tdata[MAX_THREADS]
  dword  dwThreadIdArray[MAX_THREADS]
  HANDLE hThreadArray[MAX_THREADS]
  sys    i


  'Create MAX_THREADS worker threads.
  '----------------------------------

  for i=1 to MAX_THREADS
    '
    'Generate some unique data for each thread to work with.
    '------------------------------------------------------
    '   
    tdata[i].val1=i : tdata[i].val2=i+100
    '
    'Create the thread to begin execution on its own.
    '------------------------------------------------
    '
    hThreadArray[i] = CreateThread
    ( 
    byval NULL,              'default security attributes
    0,                       'use default stack size  
    byval FunArray[i],       'thread function
    tdata[i],                'argument to thread function 
    0,                       'use default creation flags 
    dwThreadIdArray[i]       'returns the thread identifier
    )
    '
    'print "ThreadID: " dwThreadIdArray[i]
    '
    'Check the return value for success.
    'If CreateThread fails, terminate execution. 
    'This will automatically clean up threads and memory. 
    '
  next 'End of main thread creation loop.
  '
  'print "All threads up and running"
  '
  'Wait until all threads have terminated.
  '---------------------------------------
  '
  WaitForMultipleObjects MAX_THREADS, hThreadArray, TRUE, INFINITE
  '
  'Close all thread handles and free memory allocations.
  '-----------------------------------------------------
  '
  for i=1 to MAX_THREADS
    CloseHandle( hThreadArray[i] )
  next
  '
  return 0
  '
  end function 'main

  main()

  print "All thread are closed. Program will now terminate"


