Created
March 26, 2024 17:23
-
-
Save lbialy/7204176778d43972f19c65d150610eea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> 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