
extern lib "kernel32.dll"
'
void   QueryPerformanceCounter(quad*c)
void   QueryPerformanceFrequency(quad*freq)
sys    CreateThread (sys lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, *lpThreadId)
dword  WaitForMultipleObjects(sys nCount,*lpHandles, bWaitAll, dwMilliseconds)
bool   CloseHandle(sys hObject)
void   Sleep(sys dwMilliSeconds)
'
quad freq,t1,t2
QueryPerformanceFrequency freq
QueryPerformanceCounter   t1
'
% max     10000

double p1[max]
double p2[max]
double p3[max]
double p4[max]


macro FindPrimes(p)
finit
double n=1
sys c,k
do
  n+=2
  if c>=max then exit do
  '
  'IS IT DIVISIBLE BE ANY PREVIOUS PRIME
  '
  for k=1 to c
     if frac(n/p[k])=0 then exit for
  next
  '
  if k>c then
    c++
    p[c]=n
  end if
end do
end macro


function threadA(sys v) as sys
FindPrimes(p1)
end function

function threadB(sys v) as sys
FindPrimes(p2)
end function

function threadC(sys v) as sys
FindPrimes(p3)
end function

end extern

function main(sys v)
FindPrimes(p4)
end function


% threads 1

% INFINITE 0xFFFFFFFF  'Infinite timeout

sys Funs[threads]={@threadA,@threadB,@threadC}
sys  hThread[threads], id[threads], i
'
'
for i=1 to threads
  hThread[ i ] =  CreateThread 0,0,funs[ i ],i,0,id[ i ]
next


main 1


if threads>0 then
  WaitForMultipleObjects Threads, hThread, 1, INFINITE
end if

for i=1 to Threads
  CloseHandle hThread[ i ]
next


QueryPerformanceCounter t2
print str((t2-t1)/freq,3) " secs    " max "    " p1[max] 'last prime