type threaddata
char as ubyte
p as ubyte
Mutexdata as any ptr
end type
sub printchar(c as any ptr)
dim as threaddata ptr mythreaddataptr = c
dim as threaddata td = *mythreaddataptr
static p as integer
p+=1
for i as integer = 1 to 90
locate td.p,i
print chr(td.char);
sleep 1
next
end sub
sub threadbuilder
dim as any ptr thread(0 to 256)
dim as integer threadid
dim as threaddata pd
dim as threaddata ptr pdadr = @pd
dim as any ptr td = pdadr
for i as ubyte = asc("A") to asc("A")+25
with pd
.char = i
.p=threadid+1
.Mutexdata = MUTEXCREATE ' How to use this?
end with
thread(threadid)=ThreadCreate (@printchar,pdadr)
threadid +=1
sleep 1
next
dim as integer threadidend = threadid
for i as ubyte = 0 to threadidend-1
threadwait(thread(threadid))
next
end sub
threadbuilder
sleep
You must have a threaddata instance for each thread (no assumptions can be made about the order in which multiple create threads actually start executing), and a common mutex for all threads (to impose mutual exclusion on the two instructions 'locate' + 'print').
type threaddata
char as ubyte
p as ubyte
static Mutexdata as any ptr
end type
dim as any ptr threaddata.Mutexdata
sub printchar(byval c as any ptr)
dim as threaddata ptr mythreaddataptr = c
for i as integer = 1 to 70
MutexLock(mythreaddataptr->Mutexdata)
locate mythreaddataptr->p,i
print chr(mythreaddataptr->char);
MutexUnlock(mythreaddataptr->Mutexdata)
sleep 1
next
end sub
sub threadbuilder
dim as threaddata pd(0 to 19)
dim as any ptr thread(0 to 19)
threaddata.Mutexdata = MutexCreate
for i as ubyte = 0 to 19
with pd(i)
.char = asc("A") + i
.p=i + 1
end with
thread(i)=ThreadCreate (@printchar,@pd(i))
sleep 1
next
for i as ubyte = 0 to 19
threadwait(thread(i))
next
MutexDestroy(threaddata.Mutexdata)
print
end sub
threadbuilder
sleep