github.com/alphadose/zenq/v2@v2.8.4/examples/simple/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/alphadose/zenq/v2"
     8  )
     9  
    10  type payload struct {
    11  	alpha int
    12  	beta  string
    13  }
    14  
    15  func main() {
    16  	zq := zenq.New[payload](10)
    17  
    18  	for j := 0; j < 5; j++ {
    19  		go func() {
    20  			for i := 0; i < 20; i++ {
    21  				zq.Write(payload{
    22  					alpha: i,
    23  					beta:  fmt.Sprint(i),
    24  				})
    25  			}
    26  		}()
    27  	}
    28  
    29  	// For lowest latency and best performance, allocate the ZenQ.Read() calling goroutine an entire OS thread
    30  	// by calling runtime.LockOSThread()
    31  	// Note:- If you have a single core then doing this will cause a deadlock
    32  	runtime.LockOSThread()
    33  	defer runtime.UnlockOSThread()
    34  
    35  	for i := 0; i < 100; i++ {
    36  		if data, queueOpen := zq.Read(); queueOpen {
    37  			fmt.Printf("%+v\n", data)
    38  		}
    39  	}
    40  }