Using threads(pThread)

Hello.

i just downloaded the pThread library and i just wanted to try out some graphics.

So, i typed this down:

#include <posix/pthread.h>
#include <posix/sched.h>
#include <windows.h>
#include <stdio.h>
#include <gl/glut.h>


void Display(void);
void mouse(int b, int s, int x, int y);

pthread_t ntid, ntid1;

// for the command line arguments
  char **args;   
  int count;

void* fn_1(void *arg)
{
	printf("fun1
");

	/* Initialisations */
	glutInit(&count, args);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	glutInitWindowSize(500, 500);
	glutCreateWindow("Sample");

	glutDisplayFunc(Display);
	glutMouseFunc(mouse);	

	glutMainLoop();
	return ((void *)0);	
}

void Display(void)
{
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glFlush();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1, 1, -1, 1, -1, 1);
}

void mouse(int b, int s, int x, int y)
{
	// just plot a point
	glColor3f(0.0, 0.0, 0.0);
	if (b == GLUT_LEFT_BUTTON && s == GLUT_DOWN) {
		glBegin(GL_POINTS);
			glVertex2i(1, 1);
		glEnd();
		glFlush();
	}
	
}

void* fn_2(void *arg)
{
	int x = 10;
	Sleep(2000); // make thread 1 execute first(hence delay)

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	/* draw the cube here */
	while (1) {
		// this is for clearing
		glColor3f(1.0, 1.0, 1.0);
		glutSolidCube(1);

		glRotatef(x += 10, 1, 1, 1);
		glColor3f(0.0, 0.0, 0.0);
		glutSolidCube(1);

		glColor3f(1.0, 1.0, 1.0);
		glutWireCube(1);

		glFlush();
		Sleep(100);
	}	
}

int main(int argc, char **argv)
{	
	count = argc;
	args  = argv;

	/* Create the 2 threads */
	int err1 = pthread_create(&ntid, NULL, fn_1, NULL);
	int err2 = pthread_create(&ntid1, NULL, fn_2, NULL);
	
	/*-wait for threads to exit-*/
	void *val, *val1;
	pthread_join(ntid,  &val);
	pthread_join(ntid1, &val1);
	/*---------------------------*/

	return 0;
}

But im just getting a blank window.
Can anyone please tell me where im going wrong?
Thanks.

You must execute windowing commands in your main thread. Also, you cannot execute GL commands in a new thread unless the OpenGL context is current within that thread. Also, making calls to the same GL context in different threads should only be done with extreme care and caution.

I’m not familiar with pthreads, but I seriously doubt that the library ensures that the “ntid” thread will execute before the “ntid1” thread. And unless the threads execute in that order, then “ntid1” may try to execute OpenGL commands before the window is created. Which can’t work.

This is terrible threading code. Even if you did the OpenGL stuff necessary to make it run, you’ve got numerous race conditions. You need to investigate the pitfalls and issues regarding threading in general before trying to apply that to OpenGL.

Ok. i will try to learn more about threads before trying it in Graphics. Thanks for the suggestion.

Suggest trying something like boost threads.
Using low level pthreads is a bit dated (and harder), unless you have a specific reason to go with them!!