$include "windowssdk.inc" type MyNum uint64 total int startcnt int endcnt endtype SUB ET_Thread(Uint subAsThread, pointer subParam), UINT DEF threadHandle AS UINT DEF tmpHandle AS UINT threadHandle = CreateThread(0,0,subAsThread,subParam,0x4,tmpHandle) if threadHandle <> 0 then ResumeThread(threadHandle) return threadHandle ENDSUB OPENCONSOLE MyNum ThreadA MyNum ThreadB MyNum ThreadC MyNum ThreadD ThreadA.startcnt = 0 ThreadA.endcnt = 10000 ThreadA.total = 0 ThreadB.startcnt = 2500 ThreadB.endcnt = 4999 ThreadB.total = 0 ThreadC.startcnt = 5000 ThreadC.endcnt = 7499 ThreadC.total = 0 ThreadD.startcnt = 7500 ThreadD.endcnt = 10000 ThreadD.total = 0 SUB Thread1(pointer param1) 'Variables should be local or passed by parameter. UINT count SETTYPE param1, MyNum for count = #param1.startcnt to #param1.endcnt #param1.total += count sleep(1) 'need delay to show thread works next count ENDSUB uint T1, T2, T3, T4 Double StartTmr, EndTmr int flag1 = 0 UINT64 gtotal setprecision 4 Print "Starting program to total values from 0 to 10000\nwithout threading.....\n" 'show without using thread. Cumulative total from 0 to 10000 StartTmr = GetTickCount() Thread1(&ThreadA) print "Thread 1 total:" + str$(ThreadA.total) EndTmr = (GetTickCount() - StartTmr)/1000 print using("Elapsed Time: ###.####",EndTmr) + "\n" 'reset totals and show using count split between threads Print "Starting program to total values from 0 to 10000\nwith four threads splitting the work load.....\n" ThreadA.total = 0 ThreadA.endcnt = 2499 StartTmr = GetTickCount() T1 = ET_Thread(&Thread1,&ThreadA) T2 = ET_Thread(&Thread1,&ThreadB) T3 = ET_Thread(&Thread1,&ThreadC) T4 = ET_Thread(&Thread1,&ThreadD) DO IF T1<>0 if WaitForSingleObject(T1, 1) = 0 CloseHandle(T1) T1 = 0 print "Thread 1 closed:" + STR$(ThreadA.total) endif endif if T2<>0 if WaitForSingleObject(T2, 1) = 0 then CloseHandle(T2) T2 = 0 print "Thread 2 closed:" + str$(ThreadB.total) endif endif if T3<>0 if WaitForSingleObject(T3, 1) = 0 then CloseHandle(T3) T3 = 0 print "Thread 3 closed:" + str$(ThreadC.total) endif endif if T4<>0 if WaitForSingleObject(T4, 1) = 0 then CloseHandle(T4) T4 = 0 print "Thread 4 closed:" + str$(ThreadD.total) endif endif IF T1=0 and T2=0 and T3=0 and T4=0 and flag1 = 0 flag1 = 1 gtotal = ThreadA.total + ThreadB.total + ThreadC.total + ThreadD.total EndTmr = (GetTickCount() - StartTmr)/1000 print "Total:" + Str$(gtotal) Print using("Elapsed Time: ###.####",EndTmr) print "\nFinished." endif UNTIL INKEY$ <> "" if T1<>0 then CloseHandle(T1) endif if T2<>0 then CloseHandle(T2) endif if T3<>0 then CloseHandle(T3) endif if T4<>0 then CloseHandle(T4) endif CLOSECONSOLE END