Lightbulb1
Lightbulb1

Reputation: 12902

How to create a glfw thread in c++?

I just started using glfw to try and built a game. I'm fairly new to C and C++ but I worked with openGL before for android. I have got all the openGL stuff working and now started to try and make a thread with 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 -o glthread glthread.c -lglfw
$ ./glthread
test
hello

The problem is i want to take advantage of c++ features like classes is my game. When I compile in g++ i get this...

$ 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?

My main PC for working on this is an arch linux machine. I can't give the versions of my compilers right now. If it will help I can get them later.

Upvotes: 0

Views: 1731

Answers (1)

ForEveR
ForEveR

Reputation: 55897

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*). 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).

Your example works in C, since in C empty brackets (i.e. () ) means - any number of parameters of any types, but in C++ () means, that function shouln't receive parameters at all.

Upvotes: 1

Related Questions