Concurrent processing in Gox language and using channels to realize data security sharing-GX21

In the Gox language, it basically inherits the concurrency mechanism of the Go language and the channel type chan used for data security sharing operations.

Regarding the use of chan type, first look at the instructions in Qlang.

chan type

ch1 = make(chan bool, 2) // 得到 buffer = 2 的 chan bool
ch2 = make(chan int) // 得到 buffer = 0 的 chan int
ch3 = make(chan map[string]int) // 得到 buffer = 0 的 chan map[string]int

Similar to the Go language, chan has the following built-in operations:

n = len(ch1) // 取得chan当前的元素个数
m = cap(ch1) // 取得chan的容量
ch1 <- true // 向chan发送一个值
v = <-ch1 // 从chan取出一个值
close(ch1) // 关闭chan,被关闭的chan是不能写,但是还可以读(直到已经写入的值全部被取完为止)

It should be noted that after chan is closed, <-ch gets the undefined value. So it should be like this in qlang:

v = <-ch1
if v != undefined { // 判断chan没有被关闭的逻辑
	...
}

More detailed usage is shown in the following example:

c = make(chan int)

go fn() {
	for {
		c <- 1
		tk.SleepSeconds(1)
	}
}()

go fn() {
	for {
		c <- 200
		tk.SleepSeconds(1)
	}
}()

go fn() {
	for {
		tmp = <-c
		println(tmp)
		tk.SleepMilliSeconds(500)
	}
}()

for {
	tk.SleepSeconds(5)

	println("main thread")
}

In this piece of code, 3 Goroutines are created, which can be understood as 3 threads. The first two threads write data to the channel every second, and the third thread writes data from the channel every 500 milliseconds. Read a data and display it. In addition, we must not forget the main thread, the main thread must always exist, otherwise the child threads will all exit with the end of the main thread. Therefore, we have an infinite loop in our main thread, outputting information every 5 seconds.

Also note that the three Goroutines are all run with go plus an anonymous function. The usage of go is the same as in the Go language, and other functions can also be started as threads.

You can use Ctrl-C to exit the program, and the result of the entire code execution is similar to the following (it will repeat indefinitely until you press Ctrl-C to terminate):

λ gox chan.gox
1             
200           
1             
200           
1             
200           
1             
200           
1             
200           
main thread   
1             
200           
1             
200           

 


 

* Note: Since version 0.988, in order to reduce unnecessary file size, Gox has abandoned other script engines and only supports Qlang engine, so the following content is no longer valid and is only reserved for reference to the old version.

And the following is the usage of the Anko engine version, which is similar.

First of all, it is very convenient that any function can be started as a thread with the go keyword; then the channel can be defined in such a way as make (chan string), the so-called channel in the Go language is the concurrency safety for sharing data For specific data types, please refer to the official documentation of Go language for specific usage.

More detailed usage is shown in the following example:

tk = import("tk")

c = make(chan int64)

go func() {
	for {
		c <- 1
		tk.SleepSeconds(1)
	}
}()

go func() {
	for {
		c <- 200
		tk.SleepSeconds(1)
	}
}()

go func() {
	for {
		tmp = <-c
		println(tmp)
		tk.SleepMilliSeconds(500)
	}
}()

for {
	tk.SleepSeconds(5)

	println("main thread")
}

In this piece of code, 3 Goroutines are created, which can be understood as 3 threads. The first two threads write data to the channel every second, and the third thread writes data from the channel every 500 milliseconds. Read a data and display it. In addition, we must not forget the main thread, the main thread must always exist, otherwise the child threads will all exit with the end of the main thread. Therefore, we have an infinite loop in our main thread, outputting information every 5 seconds.

You can use Ctrl-C to exit the program, and the result of the entire code execution is similar to the following (it will repeat indefinitely until you press Ctrl-C to terminate):

λ gox scripts\chan.gox            
1                                 
200                               
1                                 
200                               
1                                 
200                               
1                                 
200                               
1                                 
200                               
main thread                       
1                                 
200                               
1                                 
200                               
1                                 
200                               

Guess you like

Origin blog.csdn.net/weixin_41462458/article/details/108573200