-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
syscall: cannot call Win32 API functions that spawn new threads #9240
Comments
You can't arbitrarily create new threads on Unix systems either. The Go runtime more or less has to be in charge of threading. Calling CreateThread in a Go program is not obviously useful. Is there a problem in more obviously useful code? |
If you try to implement a Windows service in Go, calling |
Is this the Windows version of issue #227? |
Not exactly. It looks like #227 involves forking a separate process, which you can do on Windows without an issue by calling |
I think this is a dup of #6751. Alex |
Sorry about previous mistake comment. package main
import (
"C"
"fmt"
"syscall"
)
func ThreadProc(p uintptr) uintptr {
fmt.Println("FOO")
return 0
}
func main() {
modkernel32 := syscall.MustLoadDLL("kernel32.dll")
procCreateThread := modkernel32.MustFindProc("CreateThread")
r1, _, _ := procCreateThread.Call(0, 0, syscall.NewCallback(ThreadProc), 0, 0, 0)
h := syscall.Handle(r1)
syscall.WaitForSingleObject(h, syscall.INFINITE)
syscall.CloseHandle(h)
} |
We actually do allow calls from non-Go-created threads, provided cgo is in use. We should probably assume cgo is in use all the time on Windows, since it effectively is. |
Dup of #6751. Alex |
It is not possible to call Win32 API functions that spawn new threads because the callback wrapper generated by
syscall.NewCallback
does not perform the needed steps to set up the call stack as needed by the Go runtime for a new thread. Attempting to call such functions will cause the new thread to hang forever. This can be easily reproduced with the following simple test program:This will hang forever in Go. Translating this program into C/C++ and compiling it with an adequate compiler such as Microsoft Visual C++ will give the expected output of printing
FOO
and terminating.The text was updated successfully, but these errors were encountered: