Win32 消息泵和 std::thread 用于创建 OpenGL 上下文和渲染
Win32 Message Pump and std::thread Used to Create OpenGL Context and Render
如果我有一个函数执行以下操作:
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);
}
由于 window 是在渲染线程上重新创建的,消息泵将在主线程上执行,这是否安全? WndProc 将调用什么函数?上面的代码可以被认为是糟糕的设计,但这不是我感兴趣的。我只对定义的行为感兴趣。
Win32 window 绑定到创建它的线程。 只有那个线程可以为window接收和发送消息,只有那个线程可以销毁window。
因此,如果您在工作线程中重新创建 window,则该线程必须接管管理 window 并发送其消息的责任。
否则,您需要将 window 的重新创建委托回您的主线程,以便它保持在最初创建它的同一线程中。
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);
}
如果我有一个函数执行以下操作:
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);
}
由于 window 是在渲染线程上重新创建的,消息泵将在主线程上执行,这是否安全? WndProc 将调用什么函数?上面的代码可以被认为是糟糕的设计,但这不是我感兴趣的。我只对定义的行为感兴趣。
Win32 window 绑定到创建它的线程。 只有那个线程可以为window接收和发送消息,只有那个线程可以销毁window。
因此,如果您在工作线程中重新创建 window,则该线程必须接管管理 window 并发送其消息的责任。
否则,您需要将 window 的重新创建委托回您的主线程,以便它保持在最初创建它的同一线程中。
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);
}