# Native Threading Obersavtions

**URL:** <https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662>\
**Category:** Native\
**Created:** [May 3, 2018, 5:26am UTC](https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662 "2018-05-03T05:26:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgalligan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kgalligan/32/5014_2.png) [@kgalligan](https://discuss.kotlinlang.org/u/kgalligan)\
**Post date:** [May 3, 2018, 5:26am UTC](https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662/1 "2018-05-03T05:26:54Z")

</div>

Here’s some sample code (updated from original with more readable code)

```kotlin
import konan.worker.*

fun main(args: Array<String>) {
    println("Starting main")

    myWorker.schedule(TransferMode.CHECKED,{UnusedWorkData("yyy")}){
        println("Running worker")
        UnusedWorkData("zzz")
    }
}

val myWorker = startWorker()
val globalOtherClass = GlobalClass()

class GlobalClass{
    init {
        println("GlobalClass init")
    }
}

data class UnusedWorkData(val a:String)

```

What would you expect the output to be? I’m trying to build a mental model of how threading in native works, but it’s a little confusing. Running this multiple times produces various output.

```nohighlight
GlobalClass init
Starting main
GlobalClass init
Running worker

```

```nohighlight
GlobalClass init
GlobalClass init
Starting main
GlobalClass init
Running worker

```

```nohighlight
GlobalClass init
Starting main
GlobalClass init
GlobalClass init
Running worker

```

```nohighlight
GlobalClass init
Starting main
GlobalClass init
GlobalClass initRunning worker

```

These are all separate runs, using the “Hello World” template sample in CLion as the starting point. They are all presumably running “HelloWorld.kexe” as an independent executable.

The pattern seems variable. I was assuming println itself wasn’t running into thread issues, but the last example, with a print but no newline, implies that might have something to do with it.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 3, 2018, 12:34pm UTC](https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662/2 "2018-05-03T12:34:43Z")

</div>

Please use “```kotlin” instead of //START SAMPLE

and “```” tags instead of //END SAMPLE

to format code

---

<div class="post-metadata">

**Author:** ![kgalligan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kgalligan/32/5014_2.png) [@kgalligan](https://discuss.kotlinlang.org/u/kgalligan)\
**Post date:** [May 3, 2018, 7:33pm UTC](https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662/3 "2018-05-03T19:33:01Z")

</div>

Some more tests. Updating the code to busy loop and wait, I get some really strange output

```kotlin
import konan.worker.*

fun main(args: Array<String>) {
    println("Starting main")

    val future = myWorker.schedule(TransferMode.CHECKED, { UnusedWorkData("yyy") }) {
        println("Running worker")
        var tempVal = 0
        for(i in 0 .. 300000)
        {
            tempVal = i + 1000
        }
        println("Worker Done")
        UnusedWorkData("zzz")
    }

    future.consume {
        println("future.consume")
    }

    println("end method")
}

val myWorker = startWorker()
val globalOtherClass = GlobalClass()

class GlobalClass{
    constructor() {
        println("GlobalClass constructor")
    }
}

data class UnusedWorkData(val a:String)

```

The output seems to create a lot of GlobalClass instances. The more loops run, the more instances, although it’s not 1to1 obviously and I’m not sure how linear it is (the number and order aren’t 100% stable).

```nohighlight
GlobalClass constructor
Starting main
GlobalClass constructor
Running worker
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
Worker Done
GlobalClass constructor
future.consume
end method

```

Another example

```nohighlight
GlobalClass constructor
GlobalClass constructor
Starting main
Running worker
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
GlobalClass constructor
Worker Done
future.consume
end method
GlobalClass constructor

```

I’m not sure what it’s doing, but it’s definitely weird.

---

<div class="post-metadata">

**Author:** ![kgalligan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kgalligan/32/5014_2.png) [@kgalligan](https://discuss.kotlinlang.org/u/kgalligan)\
**Post date:** [May 3, 2018, 11:24pm UTC](https://discuss.kotlinlang.org/t/native-threading-obersavtions/7662/4 "2018-05-03T23:24:12Z")

</div>

OK. Never mind. Creating the worker creates a new instance of state, which creates a new worker, which creates another instance of state, repeating till the main process ends apparently. I think that’s what’s happening, anyway. If I move the worker to be local in main, everything runs as expected.
