Skip to content

Instantly share code, notes, and snippets.

@lbialy
Created March 26, 2024 17:23
Show Gist options
  • Save lbialy/7204176778d43972f19c65d150610eea to your computer and use it in GitHub Desktop.
Save lbialy/7204176778d43972f19c65d150610eea to your computer and use it in GitHub Desktop.
//> using scala 3.4.0
import language.experimental.captureChecking
class DbCon:
def runUpdate(): Unit = println(s"runUpdate on ${Thread.currentThread()}")
def close(): Unit = println("closing DbCon")
def transact[A](f: DbCon^ => A): A =
val dbCon = DbCon()
val res = f(dbCon)
dbCon.close()
res
trait Fork[A]:
def join: A
def fork[A](block: -> A): Fork[A] = new Fork[A]:
@volatile var out: Option[A] = None
val thread = Thread(() => {
out = Some(block)
})
thread.start()
def join: A =
thread.join()
out.get
@main def main(): Unit =
transact { dbCon =>
Thread(() => dbCon.runUpdate()).start()
}
transact { dbCon =>
val frk = fork {
dbCon.runUpdate()
23
}
println(frk.join)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment