Win32消息泵和用于创建OpenGL上下文和渲染的std :: thread

马修·霍根(Matthew Hoggan)

如果我具有执行以下操作的功能:

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将被调用什么功能?上面的代码可能被认为是错误的设计,但这不是我感兴趣的内容。我仅对定义的行为感兴趣。

雷米·勒博(Remy Lebeau)

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

win32 std :: thread是否会泄漏内存?

来自分类Dev

从分离的std :: thread安全退出程序Win32 C ++

来自分类Dev

在 WIN32 和 WIN64 中配置浮点单元上下文

来自分类Dev

std :: thread的构造和执行

来自分类Dev

std :: thread和rvalue参考

来自分类Dev

std :: thread和异常处理

来自分类Dev

boost :: thread和std :: thread兼容性问题?

来自分类Dev

boost :: thread和std :: thread兼容性问题?

来自分类Dev

std :: thread和std :: mutex问题

来自分类Dev

boost :: thread vs std :: thread vs pthread

来自分类Dev

如何终止std :: thread?

来自分类Dev

如何使用std :: thread?

来自分类Dev

iOS上的std :: thread

来自分类Dev

如何终止std :: thread?

来自分类Dev

同步push_back和std :: thread

来自分类Dev

C ++ std :: thread和方法类

来自分类Dev

std :: thread管理:用法和最佳实践

来自分类Dev

C ++ std :: thread和方法类

来自分类Dev

同步push_back和std :: thread

来自分类Dev

使用Win32线程模型时,MinGW-w64是否开箱即用地支持std :: thread?

来自分类Dev

在opengl应用程序中使用std :: cin进行std :: thread和输入

来自分类Dev

Thread.Abort()和Thread.Suspend()之间的区别?

来自分类Dev

std :: vector的std :: thread后代

来自分类Dev

为什么在以下上下文中使用 Thread.Sleep() 以及如何避免它?

来自分类Dev

boost / thread.hpp和boost / thread / thread.hpp有什么区别?

来自分类Dev

boost / thread.hpp和boost / thread / thread.hpp有什么区别?

来自分类Dev

std :: thread类与c ++中的std :: this_thread命名空间?

来自分类Dev

使std :: thread / mutex在mingw和g ++ 4.7.2的Win7下工作

来自分类Dev

在std :: bind和std :: thread中移动语义/行为

Related 相关文章

热门标签

归档