g++ -std=c++0x main.cpp -pthread -o main -lglut -lGL
- this is how the script that I compile my code looks like.
I get an error when starting the program
terminate called after throwing an instance of std :: system_error what (): Enable multithreading to use std :: thread: Operation not permitted
if you compile g++ -std=c++0x main.cpp -pthread -o main -lglut
(there is no option -lGL
), then multithreading works, but OpenGL is -lGL
off, of course, so I compiled it to comment out everything related to opengl. It is necessary that both multithreading and OpenGL work simultaneously.
Code:
#include <GL/glut.h> #include <thread> #include <iostream> float x=-0.5; using namespace std; void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); glVertex3f(-0.5,-0.5,0.0); glVertex3f(0.0,0.5,0.0); glVertex3f(0.5,-0.5,0.0); glEnd(); glRotatef(10,0,0,1); glutSwapBuffers(); } void t1() { while(x<1) { x=x+0.1; cout << x << endl; } } int main(int argc, char **argv) { thread thr(t1); thr.join(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("ss"); glEnable(GL_MULTISAMPLE); glutDisplayFunc(renderScene); glutMainLoop(); return 0; }