Arc Forumnew | comments | leaders | submitlogin
1 point by eds 6092 days ago | link | parent

Now I have two questions about threads:

Is there a way to wait for a thread to end, other than something like

  (let th (thread (sleep 1))
    (until (dead th))
    (pr 'done))
Also, is there a way to make a thread-local variable so threads don't all try to update the same global variable? For example

  (= pid 0)
  
  (def test ()
    (let parent? (fork)
      (= pid (+ (* pid 2) (if parent? 0 1)))))
  
  (thread
    (test)
    (test)
    (test)
    (prn pid))
prints 0, 1, 12, 25, 102, 205, 822, 1645, which much larger than the expected 1, 2, 3, 4, 5, 6, 7, 8 because all the threads update the same global variable


2 points by almkglor 6092 days ago | link

I recently added thread-local variables to Anarki (last week or week before last, maybe). To use:

  (= foo (thread-local))
  (foo) ; read the variable
  (= (foo) 42) ; write the variable
Also, I've added semaphores to Anarki. If you want to wait on another thread:

  (let s (sema)
    ; thread you're waiting on
    (thread
      (sleep 1)
      (sema-post s))
    ; monitoring thread
    (sync s)
    (pr 'done))
As mentioned, Anarki only ^^

-----

2 points by eds 6092 days ago | link

Looking at 'sync documentation, it would seem that you can also just call 'sync on a thread to wait for it to end.

  (let th
    ; thread you're waiting on
    (thread
      (sleep 1))
    ; monitoring thread
    (sync th)
    (pr 'done))

-----

1 point by eds 6092 days ago | link

Thanks, that is exactly what I wanted. Although it might be nice to have a macro wrapper around 'thread-local for initializing variables

  (w/uniq void
    (mac w/thread-local (var (o val void))
      (if (is val void)
	  `(= ,var (thread-local))
	  `(do (= ,var (thread-local))
	       (= (,var) ,val)))))
then you could do

  (w/thread-local foo 0)
instead of the two step initialization

  (= foo (thread-local))
  (= (foo) 0)

-----