Multithreading

HC Overview Multithreading
Scope of variables Objects

Multithreading

The multithreading in aUCBLogo is based on the following primitives:

The constructors

Thread 
Fiber 

some queries

Threads 
countThreads getThreadId 

and a few helper functions:

GraphEnter
GraphLeave
killThread 
yield

Synchronization is still missing.

So what you can do is pretty simple: use Thread to start a new thread, which should evaluate the instructions in its argument, like in the example, which you might copy and paste into the console:

run [
	GraphLeave
	Thread [repeat 100 [fd 10 rt 3.6 waitms 100 updateGraph yield]]
	Thread [repeat 100 [fd 10 lt 3.6 waitms 100 updateGraph yield]]
	Thread [repeat 100 [print repcount waitms 100]]
	Thread [repeat 100 [print repcount*10 waitms 100]]
	waitms 100*101
	GraphEnter
]

Before you can start drawing in a thread, you should call GraphLeave once in the mein thread, because otherwise the threads are blocked, since the main thread owns the graph. When you want to draw again in the main Logo thread, you have to call GraphEnter first. Maybe this will be improved in a later version of aUCBLogo for more simplicity.

You can try to call whole programs inside a Thread, if they have their own windows and variables to work on.

The variables are a problem, indeed.

Therefore a new variables stack strategy had to be used internally: deep binding. It allows every thread to have a stack on its own, where the variables defined with local are saved.

So if you are careful and write your programs in a style which only uses local variables, then you might get working even the same Logo program running in multiple threads in parallel.

If you have less high expectations, and you only want to be able to call different Logo programs in parallel, you can use lexical scoped procedures defined with be.

be p1 steps
	be go
		forward steps
		left angle
		updateGraph
		waitms 100
	end
	angle=1
	repeat steps [go]
end
p1 100

All variables and other things, including procedures, reside inside the namespace p1 in the example. You can see it best in the Vars Window. Just click on the ViewMenu / View / Vars, then enter "bla" in the console. You should now be able to see p1 in the Vars window. If you click on it, it should expand. Then you can see the static variable "angle" and the procedure "go".

If you save all procedures of one Logo program inside a procedure defined with BE, they will create their variables in their own namespace, and so you can run them in parallel. But you cannot use to inside BE, so its a bit of a limitation though. Of course you may call procedures defined with TO from procedures defined with BE. But be careful when using variables of the caller, if the caller is defined using BE: they wont be seen by a procedure defined with TO, because in a "TO"-procedure all objects are defined at global scope. However there goes the scope operator :: handy here:

be p2 steps
	angle=1
	repeat steps [go]
end
to go
	forward p2::steps
	left p2::angle
	updateGraph
	waitms 100
end
p2 20

One problem about Thread is, it gets slow if you create hundreds of them.

But you can use Fibers to speedup your multithreaded applications, you may look at the help entry about Fiber.

Example:

References:

Scope of variables Objects