mirror of
				https://github.com/janet-lang/janet
				synced 2025-11-04 01:23:04 +00:00 
			
		
		
		
	Working example for threaded channels.
Still no marshalling more complex values.
This commit is contained in:
		
							
								
								
									
										22
									
								
								examples/threaded-channels.janet
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								examples/threaded-channels.janet
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
(def chan (ev/thread-chan 10))
 | 
			
		||||
 | 
			
		||||
(ev/spawn
 | 
			
		||||
  (ev/sleep 0)
 | 
			
		||||
  (print "started fiber!")
 | 
			
		||||
  (ev/give chan (math/random))
 | 
			
		||||
  (ev/give chan (math/random))
 | 
			
		||||
  (ev/give chan (math/random))
 | 
			
		||||
  (ev/sleep 0.5)
 | 
			
		||||
  (for i 0 10
 | 
			
		||||
    (print "giving to channel...")
 | 
			
		||||
    (ev/give chan (math/random))
 | 
			
		||||
    (ev/sleep 1))
 | 
			
		||||
  (print "finished fiber!")
 | 
			
		||||
  (:close chan))
 | 
			
		||||
 | 
			
		||||
(ev/do-thread
 | 
			
		||||
  (print "started thread!")
 | 
			
		||||
  (ev/sleep 1)
 | 
			
		||||
  (while (def x (do (print "taking from channel...") (ev/take chan)))
 | 
			
		||||
    (print "got " x " from thread!"))
 | 
			
		||||
  (print "finished thread!"))
 | 
			
		||||
		Reference in New Issue
	
	Block a user