Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to modify local variable from another thread?

I wrote the following simple application:

object Main extends App {
    var v: Int = 0
    val t = new Thread(() =>  v = 1)
    t.start()
    t.join()
    println(v) //prints 1
}

The thing that I was confused by was that we modify local variable from another thread... And that modification (made by the other thread) is visible in the main thread.

I thought local variables are always reside in a stack (stack memory the bottom is pointed to rsp register). I thought the stack memory is allocated for each thread in the application.

Upd: Even if we modify the app as follows it prints the same:

object Main {
    def main(args: Array[String]) = {
        var v: Int = 0
        val t = new Thread(() =>  v = 1)
        t.start()
        t.join()
        println(v) //prints 1
    }
}
like image 561
Some Name Avatar asked Jun 11 '26 10:06

Some Name


2 Answers

v is not a local variable in this case. It is a member of Main singletone object.

Update for the second example: Closure () => v = 1 is compiled into an anonymous class that captures all variables it depends on. Stack-allocated primitives are turned into heap-allocated objects. Here it is explained in details: How does the memory management of closures in Scala work?

like image 174
simpadjo Avatar answered Jun 14 '26 07:06

simpadjo


What this is doing is turning what appears to be a stack object into a heap object. In Java you can do this using array of 1 without adding special classes as Scala does.

public static void main(String... args) {
    int[] v = { 0 };
    Thread t = new Thread(() => v[0] = 1);
    t.start();
    t.join();
    println(v[0]); //prints 1
}
like image 45
Peter Lawrey Avatar answered Jun 14 '26 06:06

Peter Lawrey