|
-
October 21st, 2011, 07:59 AM
#2
Re: Run C++ class functions in thread
 Originally Posted by [email protected]
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 [email protected]
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|