# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1513694253 18000 # Node ID 776d882c78b8b7065159fa97e09cb16f52aac435 # Parent 5cf02601cd14422995572e51b188c7a14f81932b day 18: simplify connecting threads diff --git a/2017/day18.d b/2017/day18.d --- a/2017/day18.d +++ b/2017/day18.d @@ -5,13 +5,8 @@ import std.datetime: msecs; import std.concurrency; -void runProgram(immutable string[] opcodes, ulong pid, Tid otherProg) { - // Connect the two threads - if (otherProg == ownerTid) { - otherProg = receiveOnly!Tid(); - } else { - send(otherProg, thisTid()); - } +void runProgram(immutable string[] opcodes, ulong pid) { + auto otherProg = receiveOnly!Tid(); long ip = 0; long[char] regs = ['p': pid]; @@ -77,8 +72,12 @@ void main(string[] args) { immutable auto opcodes = File(args[1]).byLineCopy.array.idup; - auto tid1 = spawn(&runProgram, opcodes, 0, thisTid); - auto tid2 = spawn(&runProgram, opcodes, 1, tid1); + auto tid1 = spawn(&runProgram, opcodes, 0); + auto tid2 = spawn(&runProgram, opcodes, 1); + + // Connect the two threads + send(tid1, tid2); + send(tid2, tid1); // Wait for both children to let us know they're done. auto done1 = receiveOnly!(Tid, long);