Any reason to not just do:
    
    
    proc doSomeCalculations*(i, k: int): MyObj =
      result = MyObj()
      result.importantData = @[]
      
      for j in 1..i:
        sleep(j*10)
        result.importantData.add(j*k)
        if (j mod 10) == 0:
          echo "done " & $i & " values so far...
    
    
    Run

Without threads and channels?

> I've read the docs and my plan is to use threads and a channel

I can't find any (atleast up-to-date) docs on either Channel or Thread. In most 
cases - it means that feature is not well supported and you should try 
something else. See [malebolgia](https://github.com/Araq/malebolgia) or 
[weave](https://github.com/mratsim/weave) for good multithreading library.

Anyway, here is something I pieced out that could help you get an idea how to 
use channels in Nim. Note: I'm the opposite of expert on this topic, I've spent 
literally 30 minutes just now to make it work, but it does work =D :
    
    
    import std/os
    
    var T: Thread[void]
    var chan: Channel[int]
    
    proc doCalc() {.thread.} = # note that doCalc doesn't return anything
      for i in 1..100:
        sleep(100) # imitates some work
        chan.send(i)
    
    proc main =
      chan.open()
      T.createThread(doCalc)
      
      while true:
        let progress = chan.recv()
        echo progress
        if progress >= 100:
          break
       
       chan.close()
    
    main()
    
    
    Run

Or you could also use async for that.

Reply via email to