如果我具有执行以下操作的功能:
bool foo::init()
{
[Code that creates window]
std::thread run(std::bind(&foo::run, this));
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
运行在哪里定义为:
void foo::run()
{
[Code that creates initial opengl context]
[Code that recreates window based on new PixelFormat using wglChoosePixelFormatARB]
[Code that creates new opengl context using wglCreateContextAttribsARB]
do {
[Code that handles rendering]
} while (!terminate);
}
因为窗口是在渲染线程上重新创建的,并且消息泵将在主线程上执行,这样是否安全?WndProc将被调用什么功能?上面的代码可能被认为是错误的设计,但这不是我感兴趣的内容。我仅对定义的行为感兴趣。
Win32窗口绑定到创建它的线程。只有该线程才能接收和发送该窗口的消息,并且只有该线程才能破坏该窗口。
因此,如果您在工作线程中重新创建窗口,则该线程必须承担管理窗口并分发其消息的责任。
否则,您需要将重新创建窗口委托给您的主线程,以使其与最初创建它的线程相同。
bool foo::init()
{
[Code that creates window]
std::thread run(std::bind(&foo::run, this));
while (GetMessage(&msg, NULL, 0, 0)) {
if (recreate needed) {
[Code that recreates window and signals worker thread]
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void foo::run()
{
[Code that creates initial opengl context]
[Code that asks main thread to recreate window based on new PixelFormat using wglChoosePixelFormatARB, and waits for signal that the recreate is finished]
[Code that creates new opengl context using wglCreateContextAttribsARB]
do {
[Code that handles rendering]
} while (!terminate);
}
否则,请wglChoosePixelFormatARB()
在启动工作线程之前调用主线程,并将所选的PixelFormat存储在线程可以访问它的位置。
bool foo::init()
{
[Code that creates window]
[Code that 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 that creates opengl context using wglCreateContextAttribsARB]
do {
[Code that handles rendering]
} while (!terminate);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句