Multithreading 用于创建OpenGL上下文和渲染的Win32消息泵和std::线程
如果我有一个函数可以执行以下操作:Multithreading 用于创建OpenGL上下文和渲染的Win32消息泵和std::线程,multithreading,winapi,opengl,c++11,Multithreading,Winapi,Opengl,C++11,如果我有一个函数可以执行以下操作: 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); } } 其
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);
}
否则,在启动工作线程之前,在主线程中调用wglchoospexelformatarb()
,并将所选的像素格式存储在线程可以访问的位置
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);
}
我明白了要点,不过我认为在调用wglChoosePixelFormatARB之前必须先创建窗口。wgl函数依赖于HDC的存在,并且我只能在有HWND的情况下获得HDC。正在研究基于此输入重新构造代码。