summary refs log blame commit diff stats
path: root/tests/threads/threadex.nim
blob: 90119aee7c2607ad3b088e4b7cd3f2d2c1b2cab2 (plain) (tree)
1
2
3
4
5
6
7
8
9
           
                
                                          
   



                 
               
                    
                    


                          
                                  
                     
                  

             

                           
                        
             

                         
               
                           
 
                           
                        
             
                                     



                             
              
                   
              
 


                                     
                    
                    
 
           
                         

                 
discard """
  disabled: i386
  outputsub: "Just a simple text for test"
"""

type
  TMsgKind = enum
    mLine, mEof
  TMsg = object
    case k: TMsgKind
    of mEof: discard
    of mLine: data: string

var
  producer, consumer: Thread[void]
  chan: Channel[TMsg]
  printedLines = 0
  prodId: int
  consId: int

proc consume() {.thread.} =
  consId = getThreadId()
  while true:
    var x = recv(chan)
    if x.k == mEof: break
    echo x.data
    atomicInc(printedLines)

proc produce() {.thread.} =
  prodId = getThreadId()
  var m: TMsg
  var input = open("tests/dummy.txt")
  var line = ""
  while input.readLine(line):
    m.data = line
    chan.send(m)
  close(input)
  m = TMsg(k: mEof)
  chan.send(m)

open(chan)
createThread[void](consumer, consume)
createThread[void](producer, produce)
joinThread(consumer)
joinThread(producer)

close(chan)
doAssert prodId != consId
echo printedLines