Thread: (noob) Question about classes

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    (noob) Question about classes

    I have a class that as part of its initialize function needs to start a thread that is specific to the instantiation of the class.

    so for example
    Code:
    CClass* pClass;
     
    pClass = new CClass();
     
    pClass->Initialize();
    and the Class function -
    Code:
    class CClass {
         private:
              HANDLE hThread;
              DWORD ThreadId;
         public:
              void Initialize();
              DWORD WINAPI Thread(LPVOID);
         };
     
    CClass::Initialize(){
         hThread = CreateThread(NULL , 0 , &Thread , NULL , 0 , &ThreadId);
         return;
         }
     
    CClass::Thread(LPVOID lParam){
         // thread code goes here
         return 0;
         }
    My question is will the call to pClass->Initialize() properly start a thread based on the class specific function?

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    If you aren't sure, you can always specify a member function via 'this', ie this->Thread.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, it won't, and simply compiling would have told you that. You can't pass a member function where a C callback is expected.

    I recommend you use Boost.Thread for threading, as it handles member functions (via bind) properly, without you having to jump through the hoop of stub functions.
    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

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Didn't think about that, I don't typically use win32 so it didn't cross my mind lol.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's an example of a Win32 thread "object" implementation that can "bind" to class member or regular functions. It's a couple of years old, but not too far from what I still use today. Contains a bit of hoop-jumping so it'll work with MSVC 6.0.

    http://cboard.cprogramming.com/showthread.php?t=59695

    gg

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Could you please explain to me how Boost is going to fix the issue of starting a thread based on a member function? No offence, but I dont throw black boxes at problems.

    Added -

    On looking through Codeplugs code, I noticed that he declares the thread function using the static keyword, this fixed the problem with my compiler throwing an error. Thanks CP.

    Now of course its giving link errors, but I think thats just because I put the functions in the header file instead of in their own source file.

    Added- moving the functions out of the header fixed that problem too.
    Last edited by abachler; 09-21-2007 at 05:09 PM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Boost.Thread contains an internal proper starting point for the thread, which is passed a pack holding the starting point you specified. Because this pack is, basically, a Boost.Function object, it can call any callable C++ entity: normal functions, bound member functions, complicated bindings, or in fact any function object you want.
    This comes with a tiny bit of overhead, but compared to the thread starting overhead of the OS, it is negligible.
    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. question on classes
    By ssjnamek in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2005, 04:12 PM
  2. Quick Question on Classes
    By gguy85 in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2005, 02:28 AM
  3. Question about classes and inheritance
    By ~Kyo~ in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 05:36 PM
  4. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM