thread

Top  Previous  Next

Threading is a tricky subject and should be consider an advanced feature of Girder. It is very easy to hang Girder up by improper use of the functions given below. Consider yourself warned!

 

Functions

Returns

Signature

Description

 

newthread( function, parameters )

creates and runs a new thread.

Mutex Object

newmutex()

creates a new mutex.

Condition Object

newcond()

creates a new condition.

Example

thread.newthread( function( a,)

    print(a,b)

end, {"hello", "there" })

 

This example simply prints "hello   there". But it does so from a new thread. The next example is a bit more complicated. It protects a shared variable called "result" with a mutex and signals the main thread about a change to it by using a condition.

 

local m = thread.newmutex()

local c = thread.newcond()

local result = 0

 

thread.newthread( function( mutex, cond )

 

    mutex:lock()

    for i=0, 50000 do

        result =  i

    end

 

    cond:broadcast()    

    mutex:unlock()

 

end, { m,})

 

 

m:lock()

c:wait(m)

print("Thread result:" , result)

m:unlock()

 

 

As you'll be able to see is that it will print "Thread result: 50000" correctly.