Run C++ class functions in thread
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Run C++ class functions in thread

  1. #1
    Join Date
    Jun 2010
    Posts
    4

    Run C++ class functions in thread

    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:
    [/code]

    Suppose we have class rectangle
    Code:
    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:
    Code:
    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:
    Code:
    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Run C++ class functions in thread

    Quote Originally Posted by rupeshkp728@gmail.com View Post
    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.
    Quote Originally Posted by rupeshkp728@gmail.com View Post
    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

  3. #3
    Join Date
    Jan 2011
    Posts
    101

    Re: Run C++ class functions in thread

    You might want to have a look at std::thread (if you use MSVC 2010)...
    Sorry for intrusion.
    In what header is std::thread in MSVC 2010? I can't find it.

  4. #4
    Join Date
    Oct 2008
    Posts
    1,116

    Re: Run C++ class functions in thread

    there's no std::thread in vc2010, sorry

    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 ... ).

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,960

    Re: Run C++ class functions in 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.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center