nshy
nshy

Reputation: 1074

glClientWaitSync causes segmentation fault on windows

More specifically I have two threads.

The first one:

ev_wait_vsync = CreateEvent(NULL, FALSE, FALSE, "wait_vsync");
...
printf("paint\n");
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
SetEvent(ev_wait_vsync);

And second one, created by the first:

WaitForSingleObject(ev_wait_vsync, INFINITE);
printf("start vsync\n");
glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, UINT64_MAX);
printf("end sync\n");

This causes segfault. The output is:

paint
start vsync

so the problem is with glClientWaitSync, but why?

Upvotes: 0

Views: 750

Answers (1)

datenwolf
datenwolf

Reputation: 162297

so the problem is with glClientWaitSync, but why?

Because a OpenGL context can be active in only one thread at a time. Multithreaded OpenGL operation is a delicate thing and should be avoided where possible.

What happens in your case is, that glClientWaitSync must be obtained via wglGetProcAddress and the function pointers this function yields are only valid in the context and the thread which was active when the function pointer was retrieved (this is a caveat of WGL, not OpenGL). Other context, other thread, and you need to obtain a fresh/different function pointer. Also the OpenGL sync object works only with the context is has been created from.

On a side note: GL_SYNC_FLUSH_COMMANDS_BIT has nothing to do with V-Sync! It may happen as well when a glReadPixels or a glCopyTexImage2D comes along, which cause an implicit flush.

Upvotes: 1

Related Questions