hi! is it safe to use `TransferMode.UNSAFE` when w...
# kotlin-native
l
hi! is it safe to use
TransferMode.UNSAFE
when we properly synchronise everything using posix mutexes and we don’t have references to the objects in the passing worker?
for example here:
Copy code
val worker = Worker.start()
    worker.execute(
        mode = TransferMode.UNSAFE,
        producer = { Unit },
        job = {
            val a = mutableListOf<Int>()
            val worker2 = Worker.start()
            worker2.execute(
                mode = TransferMode.UNSAFE,
                producer = { a },
                job = { list ->
                    sleep(1)
                    GC.collect()
                    sleep(1)
                    list.add(1)
                    print(list)
                }
            )
        }
    )
is it guaranteed that list
a
will not be GCed after worker1 finished?
k
Why would you?
The safe/unsafe is about the producer and Unit
I’d say “yes” but would want to double check and don’t have time today…
Oh wait, the second unsafe no way
The outer one probably wouldn’t matter
l
so it means that list
a
will be collected by GC when the outer worker is finished?
k
No. The GC doesn’t really work the way you think it does. Reference counting on non-frozen state is not atomic. You’ll have memory crashes and/or leaks (potentially).
I talk about unsafe in those and why you can’t do that
a
Is there any good chance that memory model will allow sharing?
k
You can share frozen state. You cannot share “regular” mutable state. You can pass mutable state, but that will be syntactically complex in the case you’ve presented.
You can also use mutable frozen collections, but if performance is a concern, be advised it’ll be different than you’re used to. https://github.com/touchlab/Stately
a
Thanks for the readings. Do you know if there are any known plans to update the memory model so it will allow mutable state sharing?
k
That’s a long discussion. I doubt it, except in limited cases. It was designed for safer concurrency, so they’d need to “give up” on that plan. My thoughts: https://medium.com/@kpgalligan/saner-concurrency-74b0bf8ed446