Browse Source

Updated thread examples.

woollybah 6 years ago
parent
commit
e0b32389f6

+ 5 - 6
threads.mod/doc/createmutex.bmx

@@ -1,7 +1,7 @@
 
 'Make sure to have 'Threaded build' enabled!
 '
-Strict
+SuperStrict
 
 'a global list that multiple threads want to modify
 Global list:TList=New TList
@@ -11,7 +11,7 @@ Global listMutex:TMutex=CreateMutex()
 
 Function MyThread:Object( data:Object )
 
-	For Local item=1 To 10
+	For Local item:Int = 1 To 10
 		'simulate 'other' processing...
 		Delay Rand( 10,50 )
 
@@ -30,14 +30,14 @@ End Function
 Local threads:TThread[10]
 
 'Create worker threads
-For Local i=0 Until 10
+For Local i:Int = 0 Until 10
 	threads[i]=CreateThread( MyThread,String( i+1 ) )
 Next
 
 Print "Waiting for worker threads..."
 
 'Wait for worker threads to finish
-For Local i=0 Until 10
+For Local i:Int = 0 Until 10
 	WaitThread threads[i]
 Next
 
@@ -46,8 +46,7 @@ Next
 'Note: We don't really have to lock the mutex here, as there are no other threads running.
 'Still, it's a good habit to get into.
 LockMutex listMutex
-For Local t$=EachIn list
+For Local t:String = EachIn list
 	Print t
 Next
 UnlockMutex listMutex
-

+ 14 - 14
threads.mod/doc/createsemaphore.bmx

@@ -1,41 +1,41 @@
 
 'Make sure to have 'Threaded build' enabled!
 '
-Strict
+SuperStrict
 
-'a simple queue
-Global queue$[100],put,get
+' a simple queue
+Global queue:String[100],put:Int,get:Int
 
-'a counter semaphore
+' a counter semaphore
 Global counter:TSemaphore=CreateSemaphore( 0 )
 
 Function MyThread:Object( data:Object )
 
-	'process 100 items
-	For Local item=1 To 100
+	' process 100 items
+	For Local item:Int = 1 To 100
 	
-		'add an item to the queue
+		' add an item to the queue
 		queue[put]="Item "+item
 		put:+1
 		
-		'increment semaphore count.
+		' increment semaphore count.
 		PostSemaphore counter
 	
 	Next
 		
 End Function
 
-'create worker thread
+' create worker thread
 Local thread:TThread=CreateThread( MyThread,Null )
 
-'receive 100 items
-For Local i=1 To 100
+' receive 100 items
+For Local i:Int = 1 To 100
 
-	'Wait for semaphore count to be non-0, then decrement.
+	' Wait for semaphore count to be non-0, then decrement.
 	WaitSemaphore counter
 	
-	'Get an item from the queue
-	Local item$=queue[get]
+	' Get an item from the queue
+	Local item:String = queue[get]
 	get:+1
 	
 	Print item

+ 3 - 4
threads.mod/doc/createthread.bmx

@@ -1,10 +1,10 @@
 
 'Make sure to have 'Threaded build' enabled!
 '
-Strict
+SuperStrict
 
 'Custom print that shows which thread is doing the printing
-Function MyPrint( t$ )
+Function MyPrint( t:String )
 	If CurrentThread()=MainThread() 
 		Print "Main thread: "+t
 	Else
@@ -19,7 +19,7 @@ Function MyThread:Object( data:Object )
 	Myprint data.ToString()
 
 	'do some work
-	For Local i=1 To 1000
+	For Local i:Int = 1 To 1000
 		MyPrint "i="+i
 	Next
 	
@@ -35,4 +35,3 @@ Local thread:TThread=CreateThread( MyThread,"Data passed to child thread." )
 
 'wait for thread to finish and print value returned from thread
 MyPrint WaitThread( Thread ).ToString()
-