'================================================
'MULTIPLE THREADS WITH SYNC FOR STRING PROCESSING
'================================================
#define NULL 0
#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"
'================================
sys Sleep(sys msec)
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 eventA,eventB,MaxSimult
sys busy,entries
string cr=chr(13)+chr(10)
string tab=chr(10)
bstring s[8]<=("String A:" cr,"string B:" cr,"string C:" cr)
'-----------------------
function append(sys i,c)
'=======================
'do
' if busy then inc eventB : sleep 1 : else busy=1 : exit do
'end do
'
inc entries
'
'add asci char to indexed string
'
sys a,b,le,p
p=@s
mov ecx,i
dec ecx
shl ecx,2 'array index
add ecx,p
mov a,ecx 'bstring location
mov ecx,[ecx]
mov eax,[ecx-4]
mov le,eax
add le,1
b=getmemory le
copy b,*a,le
mov ecx,b
add ecx,le
dec ecx
mov al,c
mov [ecx],al
freememory *a
*a=b
'
dec entries
if entries then inc eventB
if maxSimult<entries then MaxSimult=entries
busy=0
'
end function
'-------------------------------------------
function ThreadA( dtype*pd ) as sys external
'===========================================
finit
sys i=65,j
do
append 1,i
inc eventA
inc i
sleep 1
if i>90 then i=65 : inc j : if j=4 then exit do
end do
return 10
end function
'-------------------------------------------
function ThreadB( dtype*pd ) as sys external
'===========================================
finit
sys i=97,j
do
append 2,i
inc eventA
inc i
sleep 1
if i>122 then i=97 : inc j : if j=4 then exit do
end do
return 0
end function
'-------------------------------------------
function ThreadC( dtype*pd ) as sys external
'===========================================
finit
sys i=48,j
do
append 3,i
inc eventA
inc i
sleep 1
if i>57 then i=48 : inc j : if j=8 then exit do
end do
return 0
end function
#define MAX_THREADS 3
LPTHREAD_START_ROUTINE FunArray[MAX_THREADS]<=(&ThreadA,&ThreadB,&ThreadC)
'--------------
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"
'
do
if busy then sleep 1 else busy=1 : exit do
end do
'
'
'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 s[1] cr s[2] cr s[3] cr cr "String Ops: " EventA cr+
"Total Parallel Encounters: " EventB cr+
"Max string operations in parallel: " MaxSimult
'
frees s[1]
frees s[2]
frees s[3]