YUART - How to implement this code in Spawn?...

How to implement this code in Spawn?
// V's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
import time

fn task(id int, duration int) {
println('task ${id} begin')
time.sleep(duration * time.millisecond)
println('task ${id} end')
}

fn main() {
// []thread is a special type that represents an array of threads
mut threads := []thread{}

// `spawn` starts a new thread and returns a `thread` object
// that can be added in thread array.
threads << spawn task(1, 500)
threads << spawn task(2, 900)
threads << spawn task(3, 100)

// `wait` is special function that waits for all threads to finish.
threads.wait()

println('done')
}
// V's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
import time

fn task(id int, duration int) {
println('task ${id} begin')
time.sleep(duration * time.millisecond)
println('task ${id} end')
}

fn main() {
// []thread is a special type that represents an array of threads
mut threads := []thread{}

// `spawn` starts a new thread and returns a `thread` object
// that can be added in thread array.
threads << spawn task(1, 500)
threads << spawn task(2, 900)
threads << spawn task(3, 100)

// `wait` is special function that waits for all threads to finish.
threads.wait()

println('done')
}
Solution:
Correct Spawn code is ``` // Spawn's model of concurrency is going to be very similar to Go's. // Learn more about concurrency in the documentation:...
Jump to solution
15 Replies
Petr
Petr5mo ago
Use t.assert_eq instead assert
YUART
YUART5mo ago
Oh sorry I'm stupid
Petr
Petr5mo ago
assert is not implemented yet
YUART
YUART5mo ago
Not tha code 😅 I updated the code I want to implement in Spawn
Petr
Petr5mo ago
Use runtime.Handle as array type And you need to call join in loop on this array of handles
YUART
YUART5mo ago
Ok, trying
// V's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration i32) {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
}

fn main() {
// []thread is a special type that represents an array of threads
mut threads := []runtime.Handle{}

// `spawn` starts a new thread and returns a `thread` object
// that can be added in thread array.
threads << spawn task(1, 500)
threads << spawn task(2, 900)
threads << spawn task(3, 100)

for thread in threads {
thread.join()
}

println('done')
}
// V's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration i32) {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
}

fn main() {
// []thread is a special type that represents an array of threads
mut threads := []runtime.Handle{}

// `spawn` starts a new thread and returns a `thread` object
// that can be added in thread array.
threads << spawn task(1, 500)
threads << spawn task(2, 900)
threads << spawn task(3, 100)

for thread in threads {
thread.join()
}

println('done')
}
Outputs
[🔴] × spawnc --cc clang --run x.sp
error(E0128): cannot infer type of the type parameter(s) declared on the function `join`
--> x.sp:26:9:20
|
26 | thread.join()
| ^^^^^^^^^^^

help: specify generic type(s) explicitly, for example, `foo[i32]()`
--> x.sp:26:20:20
|
26 | thread.join[]()
| ++


error: aborting due to previous error
[🔴] × spawnc --cc clang --run x.sp
error(E0128): cannot infer type of the type parameter(s) declared on the function `join`
--> x.sp:26:9:20
|
26 | thread.join()
| ^^^^^^^^^^^

help: specify generic type(s) explicitly, for example, `foo[i32]()`
--> x.sp:26:20:20
|
26 | thread.join[]()
| ++


error: aborting due to previous error
Petr
Petr5mo ago
oops. you need runtime.Handle[unit] and push instead << can you create an issue for this case? we need an error about not instantiated use of generic struct []runtime.Handle{}
YUART
YUART5mo ago
Works
Solution
YUART
YUART5mo ago
Correct Spawn code is
// Spawn's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration u64) {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
}

fn main() {
// []runtime.Handle[unit] is a special type that represents an array of threads
mut threads := []&runtime.Handle[unit]{}

// `spawn` starts a new thread and returns a `runtime.Handle[unit]` object
// that can be added in thread array.
threads.push(spawn task(1, 500))
threads.push(spawn task(2, 900))
threads.push(spawn task(3, 100))

for thread in threads {
thread.join()
}

println('done')
}
// Spawn's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration u64) {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
}

fn main() {
// []runtime.Handle[unit] is a special type that represents an array of threads
mut threads := []&runtime.Handle[unit]{}

// `spawn` starts a new thread and returns a `runtime.Handle[unit]` object
// that can be added in thread array.
threads.push(spawn task(1, 500))
threads.push(spawn task(2, 900))
threads.push(spawn task(3, 100))

for thread in threads {
thread.join()
}

println('done')
}
YUART
YUART5mo ago
Ok
Petr
Petr5mo ago
another version with results
// Spawn's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration u64) -> i32 {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
return id
}

fn main() {
// []runtime.Handle[i32] is a special type that represents an array of threads
mut threads := []&runtime.Handle[i32]{}

// `spawn` starts a new thread and returns a `runtime.Handle[i32]` object
// that can be added in thread array.
threads.push(spawn task(1, 500))
threads.push(spawn task(2, 900))
threads.push(spawn task(3, 100))

res := threads.map(|t| t.join().unwrap_or(0))
println('result: ${res}')

println('done')
}
// Spawn's model of concurrency is going to be very similar to Go's.
// Learn more about concurrency in the documentation:
// https://docs.spawnlang.dev/concepts/concurrency.html
module main

import time
import runtime

fn task(id i32, duration u64) -> i32 {
println('task ${id} begin')
time.sleep(duration * time.MILLISECOND)
println('task ${id} end')
return id
}

fn main() {
// []runtime.Handle[i32] is a special type that represents an array of threads
mut threads := []&runtime.Handle[i32]{}

// `spawn` starts a new thread and returns a `runtime.Handle[i32]` object
// that can be added in thread array.
threads.push(spawn task(1, 500))
threads.push(spawn task(2, 900))
threads.push(spawn task(3, 100))

res := threads.map(|t| t.join().unwrap_or(0))
println('result: ${res}')

println('done')
}
btw this particular example has nothing similar to the Go model as stated in the comment 😄
YUART
YUART5mo ago
Yeah, I removed that 😅
YUART
YUART5mo ago
going to be very similar to Go
Petr
Petr5mo ago
next week
Want results from more Discord servers?
Add your server