简体   繁体   English

如何在C ++中创建glfw线程?

[英]How to create a glfw thread in c++?

I just started using glfw to try and built a game. 我刚开始使用glfw尝试构建游戏。 I'm fairly new to C and C++ but I worked with openGL before for android. 我是C和C ++的新手,但之前在Android中使用过openGL。 I have got all the openGL stuff working and now started to try and make a thread with glfw. 我已经完成所有openGL的工作,现在开始尝试使用glfw创建线程。

Here is some basic test code. 这是一些基本的测试代码。 Its similar to whats in the documentation. 它类似于文档中的内容。

#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;   
}

This will compile fine in gcc and work as exspected. 这将在gcc中编译良好,并且可以按预期运行。

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

The problem is i want to take advantage of c++ features like classes is my game. 问题是我想利用c ++功能,例如类是我的游戏。 When I compile in g++ i get this... 当我用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*)’

When i put it in a class the crucial error changes to this. 当我将其放在课堂上时,关键错误就改变了。

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

What I basically want to know is, is it possible to create threads using glfw in c++ and if so how? 我基本上想知道的是,是否可以在C ++中使用glfw创建线程?

My main PC for working on this is an arch linux machine. 我主要的PC是Arch Linux机器。 I can't give the versions of my compilers right now. 我现在无法提供编译器的版本。 If it will help I can get them later. 如果有帮助的话,我稍后可以得到。

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

Should receive one argument of type void* and you cannot use class-functions here, since signature of pointer to class function is Ret (Class::*)(args) , not void (*)(void*) . 应该接收一个void*类型的参数,并且您不能在此处使用类函数,因为指向类函数的指针的签名是Ret (Class::*)(args) ,而不是void (*)(void*) If you want use pointers to class-members with threads - you should use more C++ style libraries ( boost::thread , or something like it, or write your own wrapper). 如果要使用指向带有线程的类成员的指针-您应该使用更多的C ++样式库( boost::thread或类似的东西,或者编写自己的包装器)。

Your example works in C, since in C empty brackets (ie () ) means - any number of parameters of any types, but in C++ () means, that function shouln't receive parameters at all. 您的示例在C中有效,因为在C中,空括号(即())意味着-任何数量的任何类型的参数,但在C ++()中意味着该函数根本不会接收参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM