changeset 24:776d882c78b8

day 18: simplify connecting threads
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 19 Dec 2017 09:37:33 -0500
parents 5cf02601cd14
children 836d284fff87
files 2017/day18.d
diffstat 1 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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);