OK. Here is the code: ```import kotlin.system.meas...
# kotlin-native
u
OK. Here is the code:
Copy code
import kotlin.system.measureTimeMillis
import konan.worker.*

val worker1 = startWorker()
val worker2 = startWorker()

data class Argument(val value: Int)
data class Result(val value: Long)

fun fibonacci(n: Int): Long = when (n) {
    1 -> 1L
    2 -> 1L
    else -> {
        val futures = setOf(
                // Using `n-2`instead of Argument(n-2) will be garbeled (address on Int on stack passed?)
                worker1.schedule(TransferMode.CHECKED, { Argument(n - 2) }) { argument ->
                    // Setting a breakpoint in the next line will break on ever new threads
                    // with the total number of threads in CLion's Debugger Frame window
                    // steadily increasing
                    Result(fibonacci(argument.value))
                },
                worker2.schedule(TransferMode.CHECKED, { Argument(n - 1) }) { argument ->
                    // Pairs of started/done should be printed non-interleaved, as all work is scheduled to
                    // a single worker.
                    // Actual results are interleaved though
                    println("Processing started")
                    val result = Result(fibonacci(argument.value))
                    println("Processing done")
                    result
                }
        )
        var returnVal = 0L
        var consumed = futures.size
        while (consumed > 0) {
            val ready = futures.waitForMultipleFutures(100)
            ready.forEach {
                it.consume {
                    returnVal = returnVal + it.value
                    consumed--
                }
            }
        }
        returnVal
    }
}

fun main(args: Array<String>) {
    val n = 5
    var fib = fibonacci(1)
    println("Hello, Native World!")
    fib = fibonacci(n)
    println("Fibonacci($n) = $fib")
    worker1.requestTermination().consume { _ -> }
    worker2.requestTermination().consume { _ -> }
}