multithreading - Win32 Message Pump and std::thread Used to Create OpenGL Context and Render -
if have function following:
bool foo::init() { [code creates window] std::thread run(std::bind(&foo::run, this)); while (getmessage(&msg, null, 0, 0)) { translatemessage(&msg); dispatchmessage(&msg); } }
where run defined as:
void foo::run() { [code creates initial opengl context] [code recreates window based on new pixelformat using wglchoosepixelformatarb] [code creates new opengl context using wglcreatecontextattribsarb] { [code handles rendering] } while (!terminate); }
since window recreated on rendering thread, , message pump performed on main thread considered safe? function wndproc called on? above code considered bad design, not interested in. interested in defined behavior.
a win32 window bound thread creates it. only thread can receive , dispatch messages window, , only thread can destroy window.
so, if re-create window inside of worker thread, thread must take on responsibility managing window , dispatch messages.
otherwise, need delegate re-creation of window main thread stays in same thread created it.
bool foo::init() { [code creates window] std::thread run(std::bind(&foo::run, this)); while (getmessage(&msg, null, 0, 0)) { if (recreate needed) { [code recreates window , signals worker thread] continue; } translatemessage(&msg); dispatchmessage(&msg); } } void foo::run() { [code creates initial opengl context] [code asks main thread recreate window based on new pixelformat using wglchoosepixelformatarb, , waits signal recreate finished] [code creates new opengl context using wglcreatecontextattribsarb] { [code handles rendering] } while (!terminate); }
otherwise, call wglchoosepixelformatarb()
in main thread before starting worker thread, , store chosen pixelformat thread can access it.
bool foo::init() { [code creates window] [code gets pixelformat using wglchoosepixelformatarb] std::thread run(std::bind(&foo::run, this)); while (getmessage(&msg, null, 0, 0)) { translatemessage(&msg); dispatchmessage(&msg); } } void foo::run() { [code creates opengl context using wglcreatecontextattribsarb] { [code handles rendering] } while (!terminate); }
Comments
Post a Comment