Thread seems not write to functions array Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Thread seems not write to functions array

Post by parad0x13 »

I have a function as such:

IFS.h

Code: Select all

class IFS
{
public:

	int SierpinskiTriangleIter;
	IFS()
	{
		SierpinskiTriangleIter = 10000;
	};
	~IFS(){};

	struct RGBquad
	{
		int r, g, b, a;
	};

	struct Vertex
	{
		float x, y;	// Position of vertex in 3D space
		RGBquad color;	// Color of vertex
	}*IFSList;

	void SierpinskiTriangleCoords();
}static IFS;
IFS.cpp

Code: Select all

void IFS::CoordsGraph(int equation)
{
	int iterations;
	switch(equation)
	{
	case ID_SierpinskiTriangle:
		iterations = IFS::SierpinskiTriangleIter;
		break;
	case ID_TriAngles:
		iterations = IFS::TriAnglesIter;
		break;
	case ID_Pentagon:
		iterations = IFS::PentagonIter;
		break;
	default:
		break;
	}

	glPushMatrix();
	glBegin(GL_POINTS);

	for(int d=0;d<iterations;d++)
	{
		glColor3ub(IFSList[d].color.r, IFSList[d].color.g, IFSList[d].color.b);	
		glVertex2f(IFSList[d].x, IFSList[d].y);
	}

	glEnd();
	glPopMatrix();
}

void IFS::SierpinskiTriangleCoords()
{
	free(IFSList);
	IFSList = (IFS::Vertex*)malloc(IFS::SierpinskiTriangleIter * sizeof(IFS::Vertex));	// Iteration of 500 times, reference this to elsewhere

	float pax=0, pay=-1, pbx=-1, pby=1, pcx=1, pcy=1, px=0, py=0;
	int pick, d;
	static int skip=0;

	for(d=0;d<IFS::SierpinskiTriangleIter;d++)
	{
		pick=rnd(3);
		if(pick==0)
		{
			px=(px-pax)/2;
			py=(py-pay)/2;
		}
		if(pick==1)
		{
			px=(px-pbx)/2;
			py=(py-pby)/2;
		}
		if(pick==2)
		{
			px=(px-pcx)/2;
			py=(py-pcy)/2;
		}
		if(skip>10)
		{
			IFSList[d].x = px;
			IFSList[d].y = py;
			switch(pick)        // This coloring algorithm does something compleatly unexpected!
			{
			case 0:
				IFSList[d].color.r = 255;
				IFSList[d].color.g = 0;
				IFSList[d].color.b = 0;
				IFSList[d].color.a = 255;
				break;
			case 1:
				IFSList[d].color.r = 0;
				IFSList[d].color.g = 255;
				IFSList[d].color.b = 0;
				IFSList[d].color.a = 255;
				break;
			case 2:
				IFSList[d].color.r = 0;
				IFSList[d].color.g = 0;
				IFSList[d].color.b = 255;
				IFSList[d].color.a = 255;
				break;
			default:
				break;
			}
		}
		skip++;
	}
}
and then I have my thread function that calls it:

CalculateThread.h

Code: Select all

#ifndef _CALCULATETHREAD_
#define _CALCULATETHREAD_

#include "wx.h"

DECLARE_EVENT_TYPE(wxEVT_THREAD, -1)

class CalculateThread : public wxThread
{
public:
	CalculateThread(wxEvtHandler *ehParent, int equation){m_ehParent = ehParent;Equation = equation;};
	~CalculateThread(){};

private:
	int Equation;
	wxEvtHandler *m_ehParent;
	void *Entry();
};

#endif
CalculateThread.cpp

Code: Select all

#include "CalculateThread.h"

#include "IFS.h"

void *CalculateThread::Entry()
{
	//wxCommandEvent evt(wxEVT_THREAD, wxID_ANY);
	//evt.SetId(101);
	//m_ehParent->AddPendingEvent(evt);

	IFS.SierpinskiTriangleCoords();

	return NULL;
};
Now here's the issue, when Entry is called when I create Call and Run a CalculateThread type class all runs fine except IFS.SierpinskiTriangleCoords() does not write the coordinates to the *IFSList!

Basically when I run the Coordinate Generator in a seperate thread nothing happens!

Any help? -Thanks!
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

I have trouble following your code. Unless i misread soemthing, the following should not even compile "

Code: Select all

IFS.SierpinskiTriangleCoords(); 
IFS is a class name, not an instance, and 'SierpinskiTriangleCoords' is not static, so that should not work.
"Keyboard not detected. Press F1 to continue"
-- Windows
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

At the very bottom of the IFS class a static IFS class called IFS is created:

Code: Select all

class IFS
{
public:

	int SierpinskiTriangleIter, TriAnglesIter, PentagonIter;
	IFS()
	{
		SierpinskiTriangleIter = 10000;
		TriAnglesIter = 25000;
		PentagonIter = 50000;
	};
	~IFS(){};

	struct RGBquad
	{
		int r, g, b, a;
	};

	struct Vertex
	{
		float x, y;	// Position of vertex in 3D space
		RGBquad color;	// Color of vertex
	}*IFSList;

	void SierpinskiTriangleCoords();
	void TriAnglesCoords();
	void PentagonCoords();
	void CoordsGraph(int equation);
}static IFS;
So when I call IFS.SierpinskiTriangleCoords() whatever is in that function should happen right? If I call this function in the main thread it works like a charm, but when I try to malloc from within a worker thread nothing happens. I'm wondering if I have to protect the global IFS data before I try to malliate it from within the thread

- Again, any help would be greatly appreciated
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

This is what I think is happening, when the main thread calls the static IFS class it creates an IFS class for that thread, now when the worker thread calls the static IFS thread it creates ANOTHER IFS class!

Am I correct? And if so how do I access the first threads static IFS class instead of creating another one in the worker thread?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Beware of static variables declared in a header; this variable will result in a unique instance of the class in every .cpp file that include the header - that's probably not what you want.
"Keyboard not detected. Press F1 to continue"
-- Windows
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

Thank Heavens! I didn't even realize this!

Thank you heavenly for your help : )
Post Reply