首页 文章

如何在c中创建一个glfw线程?

提问于
浏览
0

我刚开始使用glfw尝试构建游戏 . 我是C和C的新手,但我之前使用的是openGL for android . 我已经完成了所有的openGL工作,现在开始尝试用glfw创建一个线程 .

这是一些基本的测试代码 . 它类似于文档中的内容 .

#include <GL/glfw.h>
#include <stdio.h>

GLFWthread thread;

void GLFWCALL testThread()
{
    printf("hello\n");
}

int main()
{
    printf("test\n");

    glfwInit();

    thread = glfwCreateThread(testThread, NULL);
    glfwWaitThread(thread, GLFW_WAIT);

    glfwTerminate();
    return 1;   
}

这将在gcc中编译正常,并按预期工作 .

$ gcc -o glthread glthread.c -lglfw
$ ./glthread
test
hello

问题是我想利用类似c的功能是我的游戏 . 当我编译g我得到这个...

$ g++ -o glthread glthread.c -lglfw
glthread.c: In function ‘int main()’:
glthread.c:18: error: invalid conversion from ‘void (*)()’ to ‘void (*)(void*)’
glthread.c:18: error:   initializing argument 1 of ‘GLFWthread glfwCreateThread(void (*)(void*), void*)’

当我把它放在一个类中时,关键错误会改变 .

error: argument of type ‘void (Renderer::)()’ does not match ‘void (*)(void*)’

我基本上想知道的是,是否可以在c中使用glfw创建线程,如果是这样的话?

我的主要PC是一台arch linux机器 . 我现在不能给我的编译器版本 . 如果能帮助我以后再拿到它们 .

1 回答

  • 1
    void GLFWCALL testThread()
    {
        printf("hello\n");
    }
    

    应该接收一个 void* 类型的参数,你不能在这里使用类函数,因为指向类函数的指针是 Ret (Class::*)(args) ,而不是 void (*)(void*) . 如果你想使用带有线程的类成员的指针 - 你应该使用更多的C样式库( boost::thread ,或类似的东西,或编写你自己的包装器) .

    你的例子在C中工作,因为在C中空括号(即())表示 - 任何类型的任意数量的参数,但在C()中意味着,该函数根本不接收参数 .

相关问题