@@ -14,41 +14,46 @@ template runMultithreadInOrderTest*[T](chan: Chan[T]) =
1414
1515 # Launch the first worker.
1616 createThread (worker1) do :
17+ var lg = newConsoleLogger (logLevel, verboseFmtStr)
18+ lg.log (lvlDebug, " [worker1] sending message... " )
1719 chan.send (" Hello World!" )
1820
1921 # Wait for the thread to exit before moving on to the next example.
2022 worker1.joinThread ()
2123
2224 var dest = " "
2325 chan.recv (dest)
24- logger.log (lvlDebug, " Received msg: " & $ dest)
26+ logger.log (lvlDebug, " [main] Received msg: " & $ dest)
2527 doAssert dest == " Hello World!"
2628
2729
28- template runMultithreadBockTest * [T](chan: Chan [T]) =
30+ template runMultithreadNonRecvBlockTest * [T](chan: Chan [T]) =
2931 var worker2: Thread [void ]
3032 # Launch the other worker.
3133 createThread (worker2) do :
3234 # This is another proc to run in a background thread. This proc takes a while
3335 # to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
36+ var lg = newConsoleLogger (logLevel, verboseFmtStr)
3437 sleep (2000 )
38+ lg.log (lvlDebug, " [worker2] sending message... " )
3539 chan.send (" Another message" )
3640
3741 # This time, use a non-blocking approach with tryRecv.
3842 # Since the main thread is not blocked, it could be used to perform other
3943 # useful work while it waits for data to arrive on the channel.
4044
45+ logger.log (lvlDebug, " [main] running non-blocking tryRecv" )
4146 var messages: seq [string ]
4247 var msg = " "
4348 while true :
4449 let tried = chan.tryRecv (msg)
4550 if tried:
46- logger.log (lvlDebug, " Receive msg: " & $ msg)
51+ logger.log (lvlDebug, " [main] Receive msg: " & $ msg)
4752 messages.add move (msg)
4853 break
4954
5055 messages.add " Pretend I'm doing useful work..."
51- logger.log (lvlDebug, " add fakeMsg to messages (other work): " & $ messages.len ())
56+ logger.log (lvlDebug, " [main] add fakeMsg to messages (other work): " & $ messages.len ())
5257 sleep (400 )
5358
5459 # Wait for the second thread to exit before cleaning up the channel.
@@ -57,3 +62,37 @@ template runMultithreadBockTest*[T](chan: Chan[T]) =
5762 # Clean up the channel.
5863 check messages[^ 1 ] == " Another message"
5964 check messages.len >= 2
65+
66+ template runMultithreadRecvBlockTest * [T](chan: Chan [T]) =
67+ var worker2: Thread [void ]
68+ # Launch the other worker.
69+ createThread (worker2) do :
70+ # This is another proc to run in a background thread. This proc takes a while
71+ # to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
72+ var lg = newConsoleLogger (logLevel, verboseFmtStr)
73+ sleep (2000 )
74+ lg.log (lvlDebug, " [worker2] sending message... " )
75+ chan.send (" Another message" )
76+
77+ # This time, use a non-blocking approach with tryRecv.
78+ # Since the main thread is not blocked, it could be used to perform other
79+ # useful work while it waits for data to arrive on the channel.
80+
81+ logger.log (lvlDebug, " [main] running blocking recv" )
82+ var messages: seq [string ]
83+ var msg = " "
84+
85+ chan.recv (msg)
86+ logger.log (lvlDebug, " [main] Receive msg: " & $ msg)
87+ messages.add move (msg)
88+
89+ messages.add " Pretend I'm doing useful work..."
90+ logger.log (lvlDebug, " [main] add fakeMsg to messages (other work): " & $ messages.len ())
91+ sleep (400 )
92+
93+ # Wait for the second thread to exit before cleaning up the channel.
94+ worker2.joinThread ()
95+
96+ # Clean up the channel.
97+ check messages[0 ] == " Another message"
98+ check messages.len >= 2
0 commit comments