Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make a thread which will call functions of a class.
I have changed the question with a simpler example to understand the problem:
Suppose we have class rectangle
class CRectangle
{
  int x, y;
  public:
void set_values (int a,int b)
{
  x = a;
  y = b;
}

void get_values (int &a,int &b)
{
  a = x;
  b = y;
}

    int area () {return (x*y);}
};


int main ()
{
  int &x, &y;
  CRectangle rect;

  rect.set_values (3,4);

  rect.get_values (x,y);
  cout << "x = " << x << "y = " << y;

  cout << "area: " << rect.area();

  return 0;
}


Here you see that we creating an object in main fn and depending on our need calling the various fns of the class.

Now I want to make this class threadable in the sense I want to the class fns to be executed in a thread.
So I wrote the following class:
C++
<pre>class Thread
{
	Thread *threadptr;
	HANDLE    ThreadHandle;

	public:
		Thread()
		{
			Thread = 0;
		}

		void start()
		{
			DWORD threadID;

			threadptr = this;
			ThreadHandle = CreateThread(0, 0, entrypoint, threadptr, 0, &threadID);
		}

		void stop()
		{
			
		CloseHandle (ThreadHandle, 0);
		}

 		static unsigned long entrypoint(void* ptr)
		{
			((Thread*)ptr)->execute();
			return 0;
		}
		virtual void execute() = 0;
};

Usage of thread class:
class CRectangle: public Thread
{
  private:
    int x, y;
void set_values (int a,int b)
{
  x = a;
  y = b;
}

void get_values (int &a,int &b)
{
  a = x;
  b = y;
}

    int area () {return (x*y);}

	public:
		CRectangle () { }
		~ CRectangle () { }

		//the execute function, you need to override this one
		void execute()
{
//Add your threaded code here
}
	
};

int main()
{
	CRectangle obj;

	obj.start();
	obj.stop();

	return 0;
}


Now I want to know how to call ALL the various fns of class rectangle from within execute() in the same way as I called in main earlier.
Posted
Updated 20-Oct-11 22:42pm
v3

What do you mean with 'threadable'? Any thread may use the class and it's methods: all the thread needs is a reference to an instance of the class.
 
Share this answer
 
Comments
rupeshkp728 18-Oct-11 10:14am    
Thanks for the reply.
In an another fn. I will create and instance of class and call its function.
You can implement the function to execute another function in a thread, using _beginthreadex[^].
 
Share this answer
 
v2
Comments
rupeshkp728 18-Oct-11 10:13am    
Thanks Zang for the reply.
I was asking in terms of C++ which got clarified.
MFC provides a base class that you can use to do just that... CWinThread[^]. You can make a class that's derived from that then just post messages to it requesting that it execute some code.

Here's some explanation of how it works:
http://www.murrayc.com/learning/windows/multithreading.shtml[^]

Of course, this is MFC's way of doing this... if you don't want to use their classes you can always look to see how they've implemented the class and make your own (if you're really avoiding MFC).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900