Class and Thread

This is a discussion on Class and Thread within the C++ Programming forums, part of the General Programming Boards category; Hi I have a problem with using class method as a thread.Can I user a class method as a thread ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    Class and Thread

    Hi
    I have a problem with using class method as a thread.Can I user a class method as a thread beginning like;



    Object o;

    _beginthreadex(...,o.DoSomeThing,...);

    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    No, you can't. Not unless that member is static. This is due to incompatibilities of C and C++.
    (There are workarounds, though. Typically pass the address of the class as the optional param and call the appropriate function from within the static function.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Quote Originally Posted by ibaydan View Post
    Hi
    I have a problem with using class method as a thread.Can I user a class method as a thread beginning like;



    Object o;

    _beginthreadex(...,o.DoSomeThing,...);

    Thanks
    Check this out.

    Code:
    DWORD WINAPI ThreadProxy(void* arg)
    {
        Thread* thread = static_cast<Thread* >(arg);
        
        thread->Run();
        
        return 0;
    }
    
    Thread::Thread()
    {
        m_thread = CreateThread(NULL,               // default security
                                                  0,                     // default stack size
                                                  &ThreadProxy, // thread function
                                                  this,                 // thread function argument
                                                  0,                     // default creation flags
                                                  0);                   // don't want thread id
        
        if (m_thread == NULL)
            throw Exception("The thread could not be created.");
    }
    
    Thread::~Thread()
    {
        CloseHandle(m_thread);
    }
    Run() is pure virtual.
    Subclass Thread and implement Run().

    Edit:

    This is a slight alteration, on Elysia's method.
    Last edited by sethjackson; 06-02-2008 at 01:02 PM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    5

    Full Implementation

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <process.h>
    
    using namespace std;
    
    class Thread
    {
          public:
                 void Run();
                 Thread();
                 ~Thread();
                 
                 private:
                         HANDLE m_thread;
    };
    
    int main(int argc, char *argv[])
    {
        Thread thread;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    DWORD WINAPI ThreadProxy(void* arg)
    {
        Thread* thread = static_cast<Thread* >(arg);
        
        thread->Run();
        
        return 0;
    }
    
    
    void Thread::Run()
    {
         printf("Selam");
    }
    
    Thread::Thread()
    {
        m_thread = CreateThread(NULL,               // default security
                                                  0,                     // default stack size
                                                  &ThreadProxy, // thread function
                                                  this,                 // thread function argument
                                                  0,                     // default creation flags
                                                  0);                   // don't want thread id
        
        
    }
    
    Thread::~Thread()
    {
        CloseHandle(m_thread);
    }

    //Thanks

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Quote Originally Posted by ibaydan View Post
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <process.h>
    
    using namespace std;
    
    class Thread
    {
          public:
                 void Run();
                 Thread();
                 ~Thread();
                 
                 private:
                         HANDLE m_thread;
    };
    
    int main(int argc, char *argv[])
    {
        Thread thread;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    DWORD WINAPI ThreadProxy(void* arg)
    {
        Thread* thread = static_cast<Thread* >(arg);
        
        thread->Run();
        
        return 0;
    }
    
    
    void Thread::Run()
    {
         printf("Selam");
    }
    
    Thread::Thread()
    {
        m_thread = CreateThread(NULL,               // default security
                                                  0,                     // default stack size
                                                  &ThreadProxy, // thread function
                                                  this,                 // thread function argument
                                                  0,                     // default creation flags
                                                  0);                   // don't want thread id
        
        
    }
    
    Thread::~Thread()
    {
        CloseHandle(m_thread);
    }

    //Thanks
    Not quite....

    You would have to change Run() everytime you want to do something different....

    Something like this is what I was saying.....

    Code:
    virtual void Run() = 0;
    Then

    Code:
    class MyThread : public Thread
    {
    public:
    ...
        void Run();
    };
    
    void MyThread::Run()
    {
        std::cout << "Hello threaded world!" << std::endl;
    }
    
    int main(int argc, char* argv[])
    {
        MyThread thread;
    
        std::cin.get();
    
        return 0;
    }

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    I think a better interface would be a non-inheritable one.
    http://www.gotw.ca/publications/mill06.htm

    So you would need a "Thread" object that ecapsulates everything needed to run and manipulate a thread.

    gg

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about this one?
    http://www.boost.org/doc/libs/1_35_0...ml/thread.html
    The interface is largely compatible with what will presumably be part of the next C++ standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-07-2005, 11:05 PM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  3. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  4. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 07:50 AM
  5. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21