-
Notifications
You must be signed in to change notification settings - Fork 1
Threading
Splizard edited this page Nov 8, 2016
·
2 revisions
New threads can be created with the fork keyword, which allows you to start a function in a new thread.
function spin() {
loop {} //Loop forever.
}
fork spin()
print("This still prints!")
You can pass arguments like a normal function does, although you cannot return values. In order to communicate between threads you need to use pipes or the Mailbox system.
The MailBox system is a default pipe that connects your thread with its creator.
function sendHelloWorld() {
mailbox("Hello World")
}
fork sendHelloWorld()
print(mailbox())
You can also create your own pipe.
function communicateWithMe(||channel) {
print("You sent me: ", channel())
channel("I sent you this")
}
var p = ||
fork communicateWithMe(p)
p("Hello thread")
print("Reply: ", p())