demo: select

This commit is contained in:
xushiwei
2024-07-19 11:27:49 +08:00
parent 199aaf2d05
commit 5fa68f8cdd
2 changed files with 13 additions and 14 deletions

View File

@@ -1,23 +1,21 @@
package main package main
import ( import (
"unsafe"
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/os" "github.com/goplus/llgo/c/os"
"github.com/goplus/llgo/c/sys" "github.com/goplus/llgo/c/sys"
"github.com/goplus/llgo/c/syscall"
"unsafe"
) )
func main() { func main() {
var readFds syscall.FdSet var readFds sys.FdSet
sys.FD_ZERO(&readFds) sys.FD_ZERO(&readFds)
sys.FD_SET(0, &readFds) sys.FD_SET(0, &readFds)
var tv sys.TimeVal var tv sys.Timeval
tv.TvSec = 5 tv.Sec = 5
tv.TvUSec = 0 tv.Usec = 0
c.Printf(c.Str("Waiting for input on stdin...\n")) c.Printf(c.Str("Waiting for input on stdin...\n"))
ret := sys.Select(1, &readFds, nil, nil, &tv) ret := sys.Select(1, &readFds, nil, nil, &tv)

View File

@@ -1,12 +1,12 @@
package main package main
import ( import (
"unsafe"
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/net" "github.com/goplus/llgo/c/net"
"github.com/goplus/llgo/c/os" "github.com/goplus/llgo/c/os"
"github.com/goplus/llgo/c/sys" "github.com/goplus/llgo/c/sys"
"github.com/goplus/llgo/c/syscall"
"unsafe"
) )
const ( const (
@@ -41,14 +41,15 @@ func main() {
return return
} }
var writefds, readfds syscall.FdSet var writefds, readfds sys.FdSet
var timeout sys.TimeVal var timeout sys.Timeval
// Monitor socket writes // Monitor socket writes
sys.FD_ZERO(&writefds) sys.FD_ZERO(&writefds)
sys.FD_SET(sock, &writefds) sys.FD_SET(sock, &writefds)
timeout.TvSec = 10 timeout.Sec = 10
timeout.TvUSec = 0 timeout.Usec = 0
// Use select to monitor the readiness of writes // Use select to monitor the readiness of writes
if sys.Select(sock+1, nil, &writefds, nil, &timeout) > 0 { if sys.Select(sock+1, nil, &writefds, nil, &timeout) > 0 {
if sys.FD_ISSET(sock, &writefds) != 0 { if sys.FD_ISSET(sock, &writefds) != 0 {