我正在尝试在用于加载屏幕的单独线程中加载SDL2程序的纹理。我的代码看起来像这样
int batchLoad(void *ptr) {
loop through resources
SDL_LockMutex(renderMutex);
SDL_Texture *texture = NULL;
SDL_Surface *surface = IMG_Load(("../resources/" + fileName).c_str());
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
SDL_UnlockMutex(renderMutex);
// store texture
return 0;
}
void LoadingScreen::loadResources() {
// do some stuff
renderMutex = SDL_CreateMutex();
ScreenMap screenMap;
// init with data
SDL_Thread* load = SDL_CreateThread(batchLoad, "batchLoad", &screenMap);
while loading {
// do work
SDL_LockMutex(renderMutex);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, white, NULL, NULL);
// draw stuff
SDL_RenderPresent(renderer);
SDL_UnlockMutex(renderMutex);
}
SDL_WaitThread(load, NULL);
SDL_DestroyMutex(renderMutex);
SDL_DestroyMutex(satMutex);
在Windows上,这工作得很好,但是在OSX上,CreateTextureFromSurface调用出现错误。它使用空指针错误进行段错误处理。这是osx问题报告。
Thread 10 Crashed:: batchLoad
0 libGL.dylib 0x00007fff885ead32 glGenTextures + 18
Thread 10 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x00007ff913c37ad0 rcx: 0x0000000000000001 rdx: 0x00007ff913c37b50
rdi: 0x0000000000000001 rsi: 0x00007ff913c37b50 rbp: 0x00000001157ae4d0 rsp: 0x00000001157ae4d0
r8: 0x0000000000000004 r9: 0x00007ff913c00000 r10: 0x0000000023f4e094 r11: 0x000000000abea012
r12: 0x00007ff913d1e3b0 r13: 0x00007ff913d1e3b0 r14: 0x000000000000001a r15: 0x0000000000000022
rip: 0x00007fff885ead32 rfl: 0x0000000000010246 cr2: 0x0000000000000000
和lldb
* thread #9: tid = 0xee08, 0x00007fff885ead32 libGL.dylib`glGenTextures + 18, name = 'batchLoad', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: 0x00007fff885ead32 libGL.dylib`glGenTextures + 18
libGL.dylib`glGenTextures + 18:
-> 0x7fff885ead32: movq (%rax), %rdi
0x7fff885ead35: movq 0x318(%rax), %rax
0x7fff885ead3c: movl %ecx, %esi
0x7fff885ead3e: popq %rbp
当我注释掉所有线程内容并正常调用批处理加载时,没有错误,因此我认为必须存在一些并发问题。但是我对所有的渲染器调用都使用了互斥锁,所以我不确定可能是什么问题。然后我也不知道为什么它在Windows上可以正常工作,但在OSX上却不能正常工作
由于OpenGL与多线程相关的规则,因此渲染器API不能从多个线程中调用(无论是否进行了任何锁定)。在CategoryRender页面上提到了此警告,并且有指向错误报告的链接,您可以在其中阅读有关为什么这样做以及如何解决它的更多详细信息。
最简单的解决方法是将表面加载到某些背景线程上,然后将其移交给渲染器线程以加载到纹理中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句