在我的多线程应用程序中,我使用了一个 sleep() 函数(来自 GLFW 库的函数):
glfwSleep(..);
它显然导致我的应用程序出现段错误,正如我的调用堆栈所示:
#0 76CFC2BC WaitForSingleObjectEx() (C:\Windows\system32\kernel32.dll:??)
#1 00000016 ??() (??:??)
#2 0000006C ??() (??:??)
#3 00000000 ??() (??:??)
glfwSleep()
在线程内使用。那很危险吗?为什么我的程序会因此出现段错误?
编辑:
当 glfwSleep()
的参数小于 0.02(秒)时,它不会发生段错误!
编辑 2:
摘自GLFW的官方文档:
Writing threaded applications may be very awkward before you get used to it, but there are a few key rules that are fairly simple to follow:
- ALWAYS assure exclusive access to data that is shared between threads!
- Make sure that threads are synchronized properly!
- NEVER busy wait!
我想我得到了答案..现在必须找到替代方案..
谢谢!
最佳答案
来自 the GLFW wiki :
GLFW doesn't work well with GHC threads, forkIO or threadDelay. So avoid them if you can.
关于c++ - glfwSleep() 如何导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/902687/