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.
Last edited by rupeshkp728@gmail.com; October 23rd, 2011 at 02:51 PM.
Reason: giving more simpler example to understand the problem
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:
Please use code tags when posting code. It's very hard to read your code without them.
Also, check your computer for malware. You seem to have a virus that replaces normal English words with random combinations of consonants just before you submit your post.
You might want to have a look at std::thread (if you use MSVC 2010) or boost::thread. It has a lot cleaner interface than your Thread class.
Originally Posted by rupeshkp728@gmail.com
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.
You can call functions in the same way you do it from any function, regardless of which thread it is executed in. The only thing you have to be careful of is when multiple threads access the same object and at least one writes to that object. E.g. if one thread calls set_values on an instance of CRectangle and another thread calls area() on the same instance then you need to synchronize those calls using a mutex or equivalent.
You'll probably find more relevant examples if you search for some tutorials or articles about multi-threading in C++. Your example is not very useful, because there is no benefit of using multiple threads.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
you'll have to wait vc.next for the <thread> header to be implemented; in the meantime, you can use boost::thread which has a nearly equivalent interface and semantics ( but keep in mind that there are small but important differences though, just in case you're planning a successive migration to std::thread ... ).
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.
Your execute() is implemented in CRectangle, so it is in the same class scope along with all the other CRectangle member function. Therefore, those might be called directly from execute(), without any special trick.
Bookmarks